1

我创建 Windows Store 应用程序。在 MainPage.xaml.cs 我想从 xml 文件中获取数据,但我在线收到错误消息:

XDocument document = XDocument.Load(filename). 

用户代码未处理 UnauthorizedAccessException

拒绝访问路径“C:\Events Project\events.xml”。

任何建议表示赞赏。

MainPage.xaml.cs

private EventMan man = new EventMan();

public MainPage()    
{
    this.InitializeComponent();
    this.LoadEvents();
}

private void LoadEvents()
{
    this.Events = man.GetAllEvents(@"C:\Events Project\events.xml");
}

EventMan.cs

public List<Event> GetAllEvents(string filename)
{
    if (filename == null)
    {
        throw new ArgumentNullException("filename");
    }

    XDocument document = XDocument.Load(filename);

    ...    
}
4

1 回答 1

3

默认情况下,您只能使用 Windows 应用商店应用访问某些位置,如下所述:

Windows 应用商店应用程序中的文件访问和权限

您可以将文件放在应用程序的AppData文件夹中。然后您可以使用ApplicationData该类从那里自由访问文件...

var file = await ApplictionData.Current.LocalFolder.GetFileAsync("events.xml");
于 2013-06-01T11:35:00.827 回答