UWP UnauthorizedException

喜夏-厌秋 提交于 2019-12-06 04:28:17

问题


I am currently writing my first UWP app just to learn the ropes. I am building a small application that pulls data from downloaded facebook-archive.

But when I try to open the file (even though everybody has full access) I get an UnauthorizedException. I don't understand this and I haven't found anybody having this problem with just any old file (there are many people having problems with more specific scenarios but not just a simple file on their hard drive)

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:

Does anybody know why I am getting this exception? Or is it just a fact that you should not access files on your hard drive directly in UWP-Apps (which would make me question though Why they provide a file open dialog if that was the case)


回答1:


You don't show your code, but it sounds like you are trying to open the file by path rather than directly using the StorageFile returned by the file picker.

Your app doesn't have direct access to most of the file system (including the downloads directories) and can access such files only indirectly via the file system broker. The StorageFile object works with the broker to open files the use had authorized and provides a stream of the files contents for the app to read and write to.

See my blog entry http://blogs.msdn.com/b/wsdevsol/archive/2012/12/05/stray-from-the-path-stick-to-the-storagefile.aspx



来源:https://stackoverflow.com/questions/32538658/uwp-unauthorizedexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!