1

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"

And

<rescap:Capability Name="broadFileSystemAccess" />

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

  • Check in Settings -> Privacy -> File system and make sure the switch for your app is turned on. – Stefan Wick MSFT Jan 18 '20 at 20:10
  • @StefanWickMSFT yea MS just automatically breaks apps that rely on the capability with an OS update...great job. How can we reliably check if the access is turned on for the app or not? – Mehrzad Chehraz Jan 20 '20 at 22:04

2 Answers2

3

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://learn.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"));
    }
}
1

You can use the methods on the AppCapability class to query the state of the capability for the app and request access, which, may prompt the user depending on a number of geopolitical constraints that are subject to change over time. The CheckAccess() method will provide the status of the capability at the time of the call. The app can adjust it's behavior based on the results. Ideally the app would display some sort of indication to the user if it is operating in a reduced functionality mode, with a link to more details and instructions on how to enable the full functionality.