I get a System.UnauthorizedAccessException when loading XML from disk

I am working on a Windows 8 XAML / C # application. I am trying to download XML from Windows.Storage.ApplicationData.Current.LocalFolder to XDocument several times during the life of the application. However, I get a System.UnauthorizedAccessException when I try to load a file. Here are my methods:

To add to XML:

 private async void AddCategoryButton_Click(object sender, RoutedEventArgs e) { XDocument CategoryListXDoc = await LoadXmlFromLocalFolderAsync("Categories.xml"); CategoryListXDoc.Element("CategoryList").Add( new XElement("Category", new XAttribute("Id", Guid.NewGuid()), AddCategoryTextBox.Text)); SaveXDocToLocalFolderAsync("Categories.xml", CategoryListXDoc); } 

For debugging purposes only:

 private async void Button_Click_2(object sender, RoutedEventArgs e) { XDocument TempXDoc = await LoadXmlFromLocalFolderAsync("Categories.xml"); Debug.WriteLine(TempXDoc); } 

Download and save methods:

 private async Task<XDocument> LoadXmlFromLocalFolderAsync(string FileName) { var LocalFolder = Windows.Storage.ApplicationData.Current.LocalFolder; StorageFile CategoryListFile = await LocalFolder.GetFileAsync(FileName); var stream = await CategoryListFile.OpenStreamForReadAsync() as Stream; XDocument TempXDoc = XDocument.Load(stream); stream.Flush(); return TempXDoc; } private async void SaveXDocToLocalFolderAsync(string Filename, XDocument XDocToSave) { var LocalFolder = Windows.Storage.ApplicationData.Current.LocalFolder; StorageFile CategoryListFile = await LocalFolder.CreateFileAsync(Filename, CreationCollisionOption.ReplaceExisting); var stream = await CategoryListFile.OpenStreamForWriteAsync() as Stream; XDocToSave.Save(stream); stream.Flush(); } 

If I fire Click events several times, an exception is thrown. There is no specific scenario. Sometimes an error occurs, sometimes it is not. Where am I going wrong? I am new to async and await things.

0
source share
1 answer

You can check if two threads are really trying to access this file. Considering Message and StackTrace exceptions should help. Such things as

  Message=Invalid cross-thread access. StackTrace: at MS.Internal.XcpImports.CheckThread() 

should appear there.

The solution to this problem is to use locks for the LoadXmlFromLocalFolderAsync method, so two threads could not open the file at the same time. http://msdn.microsoft.com/en-us/library/a8544e2s(v=vs.100).aspx Also good advice for you is always managing files in try-catch-finally blocks.

But it gets a little dirty. The problem in interactive applications does not allow users to quickly press buttons, such as crazy ones. You must disable the functionality of the buttons until the current one is complete. Imagine a more complex operation occurs when a button is pressed; this makes the use of system resources unnecessary. It is not normal. You should try to use methods that prevent double-clicking as follows: http://tgynther.blogspot.ro/2011/07/aspnet-prevent-button-double-click.html

+1
source

All Articles