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.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.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.b__0() at System.Threading.Tasks.Task.Execute() InnerException: 

有人知道为什么我得到这个例外吗? 或者只是你不应该直接在UWP-Apps中访问你硬盘上的文件这一事实(这会让我产生疑问,但如果是这样的话,为什么他们会提供文件打开对话框)

您没有显示您的代码,但听起来您试图通过路径打开文件而不是直接使用文件选择器返回的StorageFile。

您的应用程序无法直接访问大多数文件系统(包括下载目录),只能通过文件系统代理间接访问这些文件。 StorageFile对象与代理一起使用以打开用户已授权的文件,并提供应用程序读取和写入的文件内容流。

请参阅我的博客文章http://blogs.msdn.com/b/wsdevsol/archive/2012/12/05/stray-from-the-path-stick-to-the-storagefile.aspx