UnauthorizedAccessException on writing XML to a file

北战南征 提交于 2021-02-10 16:32:02

问题


I am trying to work with some Data Binding in a Windows 8 Store App. In my project I have a Folder which I called Data. In this there's one file, Names.xml

The xml looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<names>
  <name>TestName1</name>
</names>

I can read from this file, using this method:

public async void readFromXML()
{
    var _Folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
    _Folder = await _Folder.GetFolderAsync("Data");

    var _File = await _Folder.GetFileAsync("Names.xml");

    XmlDocument document = new XmlDocument();
    var _Content = await Windows.Storage.FileIO.ReadTextAsync(_File);

    document.LoadXml(_Content);

    XmlNodeList names = document.SelectNodes("/names/name");

    foreach(var node in names)
    {
        Insert(0, new Person(node.InnerText));
    }
}

This all works and the one name does get read.

Now I want to save new data to this XML file in a similar manner (or any way that works really...)

I have no idea how I could do this, I tried this but it is not working:

public async void saveToXML()
{
    var _Folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
    _Folder = await _Folder.GetFolderAsync("Data");

    var _File = await _Folder.GetFileAsync("Names.xml");

    XmlDocument document = new XmlDocument();
    XmlElement root = document.CreateElement("names");
    document.AppendChild(root);

    XmlElement naam = document.CreateElement("name");
    naam.InnerText = "TestNaam2";
    root.AppendChild(naam);

    await Windows.Storage.FileIO.WriteTextAsync(_File, document.getXml());
}

When I try running this I get a UnauthorizedAccessException... (I am aware that the previous method will overwrite, but that wouldn't be a problem, I just want it to write in the first place.)

What am I doing wrong and how should I go about doing this? Windows 8 Apps are quiet difficult for reading and writing XML...

Thanks!


回答1:


Maybe the access to the folder

var _Folder = Windows.ApplicationModel.Package.Current.InstalledLocation;

is restricted. Try to run your app as administrator.

Or even better try to use the local storage folder for your app:

var _Folder = Windows.Storage.ApplicationData.Current.LocalFolder;


Update:

You cannot see the LocalFolder in your Visual Studio project. But you can ship the default XML file with your app and move it to LocalFolder if it doesn't exist there. Let's create a helper function:

public async System.Threading.Tasks.Task<Windows.Storage.StorageFile> GetLocalFile(string folderName, string fileName)
{
    var appLocalFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
    var appInstallLocation = Windows.ApplicationModel.Package.Current.InstalledLocation;

    var dataFolder = await appLocalFolder.TryGetItemAsync(folderName) as Windows.Storage.StorageFolder;

    if (dataFolder == null)
    {
        // the folder with given name does not exist in ApplicationData.Current.LocalFolder
        // so we have to create it
        dataFolder = await appLocalFolder.CreateFolderAsync(folderName);
    }

    var localFile = await dataFolder.TryGetItemAsync(fileName) as Windows.Storage.StorageFile;

    if (localFile == null)
    {
        // local file does not exist - so we copy it from application install folder to
        // our local data folder
        var installDataFolder = await appInstallLocation.GetFolderAsync(folderName);
        var sourceFile = await installDataFolder.GetFileAsync(fileName);
        localFile = await sourceFile.CopyAsync(dataFolder);
    }

    return localFile; 
}

And just replace this part of your readFromXML() function:

var _Folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
_Folder = await _Folder.GetFolderAsync("Data");

var _File = await _Folder.GetFileAsync("Names.xml");

With the call to our new helper function:

var _File = await GetLocalFile("Data", "Names.xml");


来源:https://stackoverflow.com/questions/21992962/unauthorizedaccessexception-on-writing-xml-to-a-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!