Search code examples
c#windows-store-apps

Access to the path is denied in Windows Store application


I create Windows Store application. In MainPage.xaml.cs I want to get data from xml file, but I get error message on line:

XDocument document = XDocument.Load(filename). 

UnauthorizedAccessException was unhandled by user code

Access to the path 'C:\Events Project\events.xml' is denied.

Any advice is appreciated.

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

    ...    
}

Solution

  • You can only access certain locations with Windows Store apps by default, as explained here:

    File access and permissions in Windows Store apps

    You can put the file in your app's AppData folder. Then you can use the ApplicationData class to freely access the file from there...

    var file = await ApplictionData.Current.LocalFolder.GetFileAsync("events.xml");