我得到System.UnauthorizedAccessException的同时从磁盘装载XML [英] I get System.UnauthorizedAccessException while loading XML from disk

查看:151
本文介绍了我得到System.UnauthorizedAccessException的同时从磁盘装载XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作在Windows 8 XAML / C#商店应用。我想的一生中多次加载从 Windows.Storage.ApplicationData.Current.LocalFolder XML以的XDocument 应用程序。不过,我收到 System.UnauthorizedAccessException的当我尝试加载该文件。这里是我的方法:

I am working on Windows 8 XAML/C# store app. I am trying to load XML from Windows.Storage.ApplicationData.Current.LocalFolder to XDocument multiple times during the lifetime of the app. However, I receive System.UnauthorizedAccessException when I try to load the file. Here are my methods:

要添加到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);
}



只是为了调试的目的:

Just for debugging purposes:

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



加载和保存方法:

Load 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();
}

如果我火了点击多次事件,抛出异常。有没有具体的方案。有时会出现错误时,有时没有。我在哪里去了?我与相当新的异步的await 的东西。

If I fire the Click events repeatedly, the exception is thrown. There's no specific scenario. Sometimes error occurs, sometimes it doesn't. Where am I going wrong? I'm pretty new with the async and await stuff.

推荐答案

您可以检查是否真正的两个线程试图访问该文件。查看异常的消息堆栈跟踪应该有所帮助。
之类的东西。

You can check if truly two threads are trying to access that file. Viewing the Message and the StackTrace of the exception should help. Things like

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



应那里出现。

should appears there.

一个解决这个问题是使用了 LoadXmlFromLocalFolderAsync 法锁,在这种方式二线程无法在同一时间打开该文件。
http://msdn.microsoft。 COM / EN-US /库/ a8544e2s(v = VS.100)的.aspx
也给你一个很好的建议是在总是管理文件的try-catch-最后块。

A solution to this problem would be to use locks for the LoadXmlFromLocalFolderAsync method, in such way two threads could not open the file in the same time. http://msdn.microsoft.com/en-us/library/a8544e2s(v=vs.100).aspx Also a good suggestion for you is to always manage files in try-catch-finally blocks.

但是,这是越来越有点脏。在交互式应用的问题是,不要让用户快速点击想疯了的按钮。直到当前已完成,则应禁用按钮功能。想象一下,发生在点击按钮更复杂的操作;这使得将要使用的系统资源不必要的。这是不正常。
你应该尝试使用防止双击这样的方法:
http://tgynther.blogspot.ro/2011/07/aspnet-prevent-button-double-click.html

But this is getting a little dirty. The problem in interactive applications is not to allow users to click quickly buttons like crazy. You should disable button functionalities until the current one is finished. Imagine more complex operations occur at button click; this makes the system resources to be used unnecessary. It is not ok. You should try to use methods that prevent double clicking like this: http://tgynther.blogspot.ro/2011/07/aspnet-prevent-button-double-click.html

这篇关于我得到System.UnauthorizedAccessException的同时从磁盘装载XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆