【发布时间】:2019-09-09 21:20:25
【问题描述】:
我有使用 Windows FolderPicker 的代码。更新到 Windows 版本 10.0.18362 后,我对 FoldePicker 的使用已停止工作。
我附上了一些我用来获得访问文件被拒绝结果的代码。
using System;
using System.IO;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.Storage.Pickers;
using Windows.Storage;
using Windows.Storage.AccessCache;
namespace FolderPickerTest
{
public sealed partial class MainPage : Page
{
private static string path = @"filepath";
string[] lines;
public MainPage()
{
this.InitializeComponent();
test();
}
public async void test()
{
var folderPicker = new FolderPicker();
folderPicker.SuggestedStartLocation = PickerLocationId.Desktop;
folderPicker.FileTypeFilter.Add("*");
StorageFolder folder = await folderPicker.PickSingleFolderAsync();
if (folder != null)
{
StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", folder);
}
}
private void open_click(object sender, RoutedEventArgs e)
{
lines = new string[2];
try
{
lines = File.ReadAllLines(path);
}
catch(Exception ex)
{
errorText.Text = ($"Error: {ex.Message.ToString()}");
}
}
}
}
我目前收到的错误消息是:
Access to the path 'filepath' is denied
【问题讨论】:
-
你从哪里得到错误?
-
您是否尝试过使用真实文件路径的代码?
-
在UWP中,禁止直接通过路径访问文件。请使用
FileOpenPicker选择文件。你可以参考我的回答here,它做你想做的。