UWP 未授权异常
UWP UnauthorizedException
我目前正在编写我的第一个 UWP 应用程序,只是为了学习一些技巧。我正在构建一个从下载的 facebook-archive 中提取数据的小型应用程序。
但是当我尝试打开文件时(即使每个人都具有完全访问权限)我得到了一个 UnauthorizedException。我不明白这一点,我还没有发现任何人对任何旧文件都有这个问题(有很多人对更具体的场景有问题,而不仅仅是他们硬盘上的一个简单文件)
System.UnauthorizedAccessException was unhandled by user code
HResult=-2147024891
Message=Access to the path 'C:\Users\patri\Downloads\facebook-100004420950389\html\messages.htm' is denied.
Source=System.IO.FileSystem
StackTrace:
at System.IO.WinRTIOExtensions.<TranslateWinRTTaskCore>d__1`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.IO.WinRTFileSystem.<OpenAsync>d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.IO.WinRTFileSystem.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, FileStream parent)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
at System.Xml.XmlSystemPathResolver.GetEntity(Uri uri, String role, Type typeOfObjectToReturn)
at System.Xml.XmlTextReaderImpl.FinishInitUriString()
at System.Xml.XmlReaderSettings.CreateReader(String inputUri, XmlParserContext inputContext)
at System.Xml.Linq.XDocument.Load(String uri, LoadOptions options)
at Facebookalyzer.FacebookArchiveParser.PopulateArchiveFromHtml(String filePath, FacebookMessageArchive archive)
at Facebookalyzer.FacebookArchiveParser.<>c__DisplayClass0_0.<ExtractAllChatMessages>b__0()
at System.Threading.Tasks.Task.Execute()
InnerException:
有人知道我为什么会收到此异常吗?或者您不应该直接在 UWP-Apps 中访问硬盘驱动器上的文件只是一个事实(这会让我质疑如果是这种情况为什么他们会提供文件打开对话框)
你没有显示你的代码,但听起来你试图通过路径打开文件,而不是直接使用文件选择器返回的 StorageFile。
您的应用无法直接访问大部分文件系统(包括下载目录),只能通过文件系统代理间接访问此类文件。 StorageFile 对象与代理一起打开用户已授权的文件,并提供文件内容流供应用程序读取和写入。
我目前正在编写我的第一个 UWP 应用程序,只是为了学习一些技巧。我正在构建一个从下载的 facebook-archive 中提取数据的小型应用程序。
但是当我尝试打开文件时(即使每个人都具有完全访问权限)我得到了一个 UnauthorizedException。我不明白这一点,我还没有发现任何人对任何旧文件都有这个问题(有很多人对更具体的场景有问题,而不仅仅是他们硬盘上的一个简单文件)
System.UnauthorizedAccessException was unhandled by user code
HResult=-2147024891
Message=Access to the path 'C:\Users\patri\Downloads\facebook-100004420950389\html\messages.htm' is denied.
Source=System.IO.FileSystem
StackTrace:
at System.IO.WinRTIOExtensions.<TranslateWinRTTaskCore>d__1`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.IO.WinRTFileSystem.<OpenAsync>d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.IO.WinRTFileSystem.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, FileStream parent)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
at System.Xml.XmlSystemPathResolver.GetEntity(Uri uri, String role, Type typeOfObjectToReturn)
at System.Xml.XmlTextReaderImpl.FinishInitUriString()
at System.Xml.XmlReaderSettings.CreateReader(String inputUri, XmlParserContext inputContext)
at System.Xml.Linq.XDocument.Load(String uri, LoadOptions options)
at Facebookalyzer.FacebookArchiveParser.PopulateArchiveFromHtml(String filePath, FacebookMessageArchive archive)
at Facebookalyzer.FacebookArchiveParser.<>c__DisplayClass0_0.<ExtractAllChatMessages>b__0()
at System.Threading.Tasks.Task.Execute()
InnerException:
有人知道我为什么会收到此异常吗?或者您不应该直接在 UWP-Apps 中访问硬盘驱动器上的文件只是一个事实(这会让我质疑如果是这种情况为什么他们会提供文件打开对话框)
你没有显示你的代码,但听起来你试图通过路径打开文件,而不是直接使用文件选择器返回的 StorageFile。
您的应用无法直接访问大部分文件系统(包括下载目录),只能通过文件系统代理间接访问此类文件。 StorageFile 对象与代理一起打开用户已授权的文件,并提供文件内容流供应用程序读取和写入。