适用于 UWP 的广泛文件系统访问不起作用 [英] broadFileSystemAccess for UWP not working

查看:20
本文介绍了适用于 UWP 的广泛文件系统访问不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm writing an app which needs a permission for accessing a text file cuz without permission it throws an exception "access denied". I added to the Package.appxmanifest specific lines

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

"IgnorableNamespaces="uap mp rescap"

还有

<rescap:Capability Name="broadFileSystemAccess" />

但是还是不行.有没有其他方法可以使用选择器访问特定文件?

But still it doesn't work. Is there any other way to access specific file with picker?

推荐答案

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

Yes the behavior changed between the April 2018 and October 2018 releases, and the default is now Disabled. This is a privacy constraint - we're very focused on maintaining the user's privacy. The documentation for this is up-to-date: https://docs.microsoft.com/en-us/windows/uwp/files/file-access-permissions#accessing-additional-locations. As of right now, if you want to detect whether the setting is enabled or disabled, you can simply try to access some file/folder to which this setting would grant you permission if enabled and deny permission if disabled (eg, "C:\"). If disabled, you can then launch the Settings app on the File System privacy page. For example:

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"));
    }
}

这篇关于适用于 UWP 的广泛文件系统访问不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆