从磁盘加载 XML 时出现 System.UnauthorizedAccessException [英] I get System.UnauthorizedAccessException while loading XML from disk

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

问题描述

我正在开发 Windows 8 XAML/C# 商店应用程序.我试图在应用程序的生命周期内多次将 XML 从 Windows.Storage.ApplicationData.Current.LocalFolder 加载到 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);
}

仅用于调试目的:

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

加载和保存方法:

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

如果我反复触发 Click 事件,则会引发异常.没有具体的场景.有时会发生错误,有时不会.我哪里错了?我对 asyncawait 的东西很陌生.

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.

推荐答案

您可以检查是否真的有两个线程正在尝试访问该文件.查看异常的 MessageStackTrace 应该会有所帮助.像

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()

应该出现在那里.

解决这个问题的方法是为 LoadXmlFromLocalFolderAsync 方法使用锁,这样两个线程就不能同时打开文件.http://msdn.microsoft.com/en-us/library/a8544e2s(v=vs.100).aspx还有一个对您的好建议是始终管理 try-catch-finally 块中的文件.

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

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

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