C# · 12月 27, 2021

c# – 尝试在InstalledLocation StorageFolder中创建文件同步时访问被拒绝?

尝试在InstalledLocation StorageFolder中CreateFileAsync时,我的Access被拒绝 StorageFolder storageFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;StorageFile file = await storageFolder.CreateFileAsync(“fileNmae”,Windows.Storage.CreationCollisionOption.ReplaceExisting);

我也试过

var storageFolder = await StorageFolder.GetFolderFromPathAsync(“ms-appx:///”);

并且“价值不在预期范围内”

我可以在Windows.Storage.ApplicationData.Current.LocalFolder中创建CreateFileAsync,然后将CopyAsync复制到InstalledLocation StorageFolder?

StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;StorageFile file = await storageFolder.CreateFileAsync(“fileName”,Windows.Storage.CreationCollisionOption.ReplaceExisting);StorageFolder installedLocationFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;var result = await file.CopyAsync(installedLocationFolder,”fileName”,Windows.Storage.NameCollisionOption.ReplaceExisting);

但是在InstalledLocation StorageFolder中的CreateFileAsync允许Access被拒绝?
那是因为安全原因还是我在这里编码错了?

解决方法 该应用的安装目录是只读位置.另外,不建议您将数据文件写入安装位置.如果您需要存储数据,仅供应用程序使用,您应该使用 StorageFolder localFolder = ApplicationData.Current.LocalFolder;

要么

Windows.Storage.StorageFolder temporaryFolder = ApplicationData.Current.TemporaryFolder;

取决于数据的生命周期.