UWP 未经授权异常

4

我现在正在编写我的第一个UWP应用程序,以便学习使用方法。我正在构建一个小型应用程序,从下载的Facebook存档中获取数据。

但是当我尝试打开文件时(即使每个人都拥有完全访问权限),我会收到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应用程序中不应该直接访问硬盘上的文件(这会让我产生疑问,为什么他们会提供文件打开对话框)

1个回答

10

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

您的应用程序不能直接访问大多数文件系统(包括下载目录),只能通过文件系统代理间接访问此类文件。 StorageFile对象与代理一起工作以打开用户已授权的文件,并为应用程序提供文件内容的流以便读写。

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


非常感谢。那正是问题所在。我以为我可以像在常规桌面应用程序中一样打开文件! - Patrick Braunstorfer
你好,我使用我的应用程序LocalFolder创建文件,但出现了UnauthorizedException异常。@Rob Caplan - MSFT - lindexi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接