我正在编写一个应用程序,它需要访问文本文件的权限,因为未经许可它会抛出异常“访问被拒绝”。
我添加到 Package.appxmanifest 特定行
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities
"IgnorableNamespaces="uap mp rescap"
And
<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"));
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)