首页 > 解决方案 > UWP 对已安装位置的访问被拒绝

问题描述

我想在 InstalledLocation 的文件中创建一个文件来保存下载的 json 字符串。但它给出了一个例外System.UnauthorizedAccessException in System.Private.CoreLib.dll。broadFileAccess Permissions 也不起作用,我也尝试了 runFullTrust。

我在行中得到错误

var NewFile = await localFolder.CreateFileAsync(@"DownloadedUpdate.json");

这是我的代码

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using static Osumen_ChatKnoladgeBase.Trainer.ModelHandler;
using Windows.Storage;
using Newtonsoft.Json;
using System.Diagnostics;
using System.Threading.Tasks;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238

namespace OsumenChatAI
{
    public class UpdateData
    {
        public object SentFrom { get; set; }
        public object Data { get; set; }
    }
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class UpdateInfo : Page
    {
        public UpdateInfo()
        {
            this.InitializeComponent();
        }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        var param = (UpdateData)e.Parameter;
        var page = param.SentFrom as MainPage;
        var NewUpdate = param.Data as Intent[];

        Task.Run(async()=> await ApplyUpdate(page, NewUpdate));

    }

    private async Task ApplyUpdate(MainPage page, Intent[] intents)
    {
        var localFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
        var folderPath = localFolder.Path;
        var NewFile = await localFolder.CreateFileAsync(@"DownloadedUpdate.json");
    }
}

}

标签: c#uwp

解决方案


我想在 InstalledLocation 的文件中创建一个文件来保存下载的 json 字符串。但它在 System.Private.CoreLib.dll 中给出了 System.UnauthorizedAccessException 异常。

在 UWP 平台中,应用的InstalledLocation 目录是只读位置。它用于部署应用程序代码和资产。

如果您需要访问创建的文件,则不能将其放在应用程序的InstalledLocation. 请使用应用程序LocalFolder替换。

StorageFolder localFolder = ApplicationData.Current.LocalFolder;

有关更多信息,请参阅应用程序数据位置 官方文档。


推荐阅读