Search code examples
c#xmlxamlwindows-8windows-store-apps

UnauthorizedAccessException when creating XML file


I'm working on a Windows 8.1 XAML/C# Metro app.

Currently, I am getting an authorization error when trying to save an XML document. Here is the method that when run throws:

   public async void saveXML(string fileName)
   {
       Windows.Storage.StorageFolder sf = await Windows.ApplicationModel.Package.Current.InstalledLocation.CreateFolderAsync("XML");
       StorageFile st = await sf.CreateFileAsync(fileName);
       await SyncXMLDoc.SaveToFileAsync(st);
   }

Here is the specific error:

  • $exception {"Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))"} System.Exception {System.UnauthorizedAccessException}

How do I resolve this? Why would I not be allowed to create an XML file?


Solution

  • You can not write files to the application's install directory. From the File access and permissions article on MSDN:

    The app's install directory is a read-only location.

    You have to save your files to one of the Application Data Folders, also explained in that article.