简体   繁体   中英

Save string to xml file (UWP) c#

I am trying to save an string to an xml file and am recieving the following error:

An exception of type 'System.UnauthorizedAccessException' occurred in System.IO.FileSystem.dll but was not handled in user code Additional information: Access to the path 'c:\\users\\brandon\\documents\\visual studio 2015\\Projects\\UniversalTestApp\\UniversalTestApp\\bin\\x86\\Debug\\AppX\\xmlfile.xml' is denied.

Reading further into the issue it would seem that I don't have the approriate permissions to complete this save. How would I go about

    private async void btnSubmit_Click(object sender, RoutedEventArgs e)
    {
        var bookmark = new Bookmark();
        bookmark.Button = cmbButton.SelectedIndex;
        bookmark.Name = txtName.Text;
        bookmark.URL = txtURL.Text;
        string output = SerializeToXml(bookmark);
        XmlDocument xdox = new XmlDocument();
        File.WriteAllText("xmlfile.xml",output);        
    }

As it turns out the reason for the error message is that you can only access certain file system locations by default in UWP. I made some modifications to my code and it is now working correctly. Thanks to those who tried to help.

    private async void btnSubmit_Click(object sender, RoutedEventArgs e)
    {
        var bookmark = new Bookmark();

        StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
        StorageFile createFile = await storageFolder.CreateFileAsync("bookmark.xml", CreationCollisionOption.ReplaceExisting);

        bookmark.Button = cmbButton.SelectedIndex;
        bookmark.Name = txtName.Text;
        bookmark.URL = txtURL.Text;
        var output = SerializeToXml(bookmark);

        StorageFile sampleFile = await storageFolder.GetFileAsync("bookmark.xml");
        await FileIO.WriteTextAsync(sampleFile, output);   
    }

尝试以“以管理员身份”运行您的应用程序

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM