【问题标题】:Save string to xml file (UWP) c#将字符串保存到 xml 文件 (UWP) c#
【发布时间】:2016-06-19 09:57:56
【问题描述】:

我正在尝试将字符串保存到 xml 文件并收到以下错误:

System.IO.FileSystem.dll 中出现“System.UnauthorizedAccessException”类型的异常,但未在用户代码中处理 附加信息:对路径“c:\users\brandon\documents\visual studio 2015\Projects\UniversalTestApp\UniversalTestApp\bin\x86\Debug\AppX\xmlfile.xml”的访问被拒绝。

进一步阅读该问题,我似乎没有完成此保存的适当权限。我该怎么办

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

【问题讨论】:

标签: c# uwp


【解决方案1】:

事实证明,错误消息的原因是在 UWP 中默认情况下您只能访问某些文件系统位置。我对我的代码进行了一些修改,现在它可以正常工作了。感谢那些试图提供帮助的人。

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 诚然,在我尝试之前,我认为你是对的。虽然没有工作。感谢您的建议。
    猜你喜欢
    • 1970-01-01
    • 2021-03-29
    • 1970-01-01
    • 2012-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多