locked
Access to the path is denied

    Question

  • Hi, I want to get data from xml file. Windows Runtime Component in C# is called by JavaScript, but I get error in Windows Runtime Component. 


    An exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll but was not handled in user code

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


    default.js

    var eventMan = new RuntimeComponent.EventMan().getAllEvents();

    EventMan.cs

    public IList<Event> GetAllEvents()
    {
    	...
    
                XDocument document = XDocument.Load(@"C:\Events Project\events.xml");
    
    	...
    }


    Saturday, June 1, 2013 8:10 AM

Answers

  • You can not read file from that location ("C:\Events Project\") due to security reasons. If you want you can keep this file in "My Documents" folder and add Documents Library capability in package.appxmanifest and then use code like this

    public async Task<IList<Event>> GetAllEvents()
    {
        try
        {
            ...
    
            var file = await Windows.Storage.KnownFolders.DocumentsLibrary.GetFileAsync("events.xml");
    
            var xml = await Windows.Storage.FileIO.ReadTextAsync(file);
    
            XDocument document = XDocument.Parse(xml);
    
            ...
        }
        catch (Exception error)
        {
            System.Diagnostics.Debug.WriteLine(error.Message);
        }
    
    }



    -- Vishal Kaushik --

    Please 'Mark as Answer' if my post answers your question and 'Vote as Helpful' if it helps you.

    Happy Coding!!!


    • Edited by Vishal Kaushik Saturday, June 1, 2013 1:14 PM
    • Marked as answer by GSarge Saturday, June 1, 2013 7:05 PM
    Saturday, June 1, 2013 1:13 PM