适用于 UWP 的广泛文件系统访问不起作用



我正在编写一个应用程序,该应用程序需要未经许可访问文本文件的权限,因为它会引发异常"访问被拒绝"。 我添加到 Package.appxmanifest 特定行

xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities
"IgnorableNamespaces="uap mp rescap"

<rescap:Capability Name="broadFileSystemAccess" />

但它仍然不起作用。还有其他方法可以使用选择器访问特定文件吗?

是的,行为在 2018 年 4 月和 2018 年 10 月版本之间发生了变化,默认值现在为"已禁用"。这是一个隐私约束 - 我们非常专注于维护用户的隐私。这方面的文档是最新的:https://learn.microsoft.com/en-us/windows/uwp/files/file-access-permissions#accessing-additional-locations。截至目前,如果您想检测该设置是启用还是禁用,您可以简单地尝试访问某些文件/文件夹,如果启用,此设置将授予您权限,如果禁用则拒绝权限(例如,"C:\"(。如果禁用,则可以在"文件系统隐私"页面上启动"设置"应用。例如:

protected override async void OnNavigatedTo(NavigationEventArgs e)
{
try
{
StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(@"C:");
// do work
}
catch
{
MessageDialog dlg = new MessageDialog(
"It seems you have not granted permission for this app to access the file system broadly. " +
"Without this permission, the app will only be able to access a very limited set of filesystem locations. " +
"You can grant this permission in the Settings app, if you wish. You can do this now or later. " +
"If you change the setting while this app is running, it will terminate the app so that the " +
"setting can be applied. Do you want to do this now?",
"File system permissions");
dlg.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(InitMessageDialogHandler), 0));
dlg.Commands.Add(new UICommand("No", new UICommandInvokedHandler(InitMessageDialogHandler), 1));
dlg.DefaultCommandIndex = 0;
dlg.CancelCommandIndex = 1;
await dlg.ShowAsync();
}
}
private async void InitMessageDialogHandler(IUICommand command)
{
if ((int)command.Id == 0)
{
await Launcher.LaunchUriAsync(new Uri("ms-settings:privacy-broadfilesystemaccess"));
}
}

可以使用 AppCapability 类上的方法查询应用功能的状态并请求访问权限,这可能会根据随时间变化的地缘政治约束来提示用户。CheckAccess(( 方法将在调用时提供功能的状态。应用可以根据结果调整其行为。理想情况下,如果应用在缩减功能模式下运行,它将向用户显示某种指示,并提供指向有关如何启用完整功能的更多详细信息和说明的链接。

最新更新