c# - 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");
}
}
}
解决方案
我想在 InstalledLocation 的文件中创建一个文件来保存下载的 json 字符串。但它在 System.Private.CoreLib.dll 中给出了 System.UnauthorizedAccessException 异常。
在 UWP 平台中,应用的InstalledLocation
目录是只读位置。它用于部署应用程序代码和资产。
如果您需要访问创建的文件,则不能将其放在应用程序的InstalledLocation
. 请使用应用程序LocalFolder
替换。
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
有关更多信息,请参阅应用程序数据位置 官方文档。
推荐阅读
- excel - 如何在 Excel 中随机分配任务给人员:(a)每个人可以执行不同数量的任务,(b)有些人不能执行某些任务
- python - 向 iplot 条形图添加水平平均线
- html - 未捕获的 ReferenceError:未定义多选
- amazon-web-services - 如何在 AWS CloudShell 中创建配置文件以访问不同的角色?
- sql-server - 是否可以使用 php sqlsrv 从存储过程执行中获得多个活动结果集?
- excel - VBA:从列表中替换工作表上的单元格值,然后导出为 PDF
- amazon-web-services - 使用 CloudFormation 创建 RDS 免费套餐实例?
- angular - Angular 调用一个帖子并接听电话,然后在上一个 get 调用完成后再次发帖并接听电话
- python - 如何获得参数以忽略部分消息
- c++ - fmt VS2019 警告,我需要关心什么?