Patrick Braunstorfer
Patrick Braunstorfer

Reputation: 383

UWP UnauthorizedException

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)

Upvotes: 4

Views: 3863

Answers (1)

Rob Caplan - MSFT
Rob Caplan - MSFT

Reputation: 21889

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

Upvotes: 10

Related Questions