1

The following code is taken from Microsoft's documentation on the subject with slight modification:

var folderPicker = new Windows.Storage.Pickers.FolderPicker();
folderPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop;
folderPicker.FileTypeFilter.Add("*");

Windows.Storage.StorageFolder folder = await folderPicker.PickSingleFolderAsync();
if (folder != null)
{
    // Application now has read/write access to all contents in the picked folder
    // (including other sub-folder contents)
    Windows.Storage.AccessCache.StorageApplicationPermissions.
    FutureAccessList.AddOrReplace("PickedFolderToken", folder);

    if (Directory.Exists(folder.Path)) // fails, I don't have permission to read this folder even though the documentation suggests that I should have access
    {
        this.textBlock.Text = "Picked folder: " + folder.Name; // never gets printed
    }
}

I also tried adding the broadFileSystemAccess capability to the application manifest. What am I missing here?

Samaursa
  • 16,527
  • 21
  • 89
  • 160

2 Answers2

0

broadFileSystemAccess only works with Windows.Storage namespace, and this is already mentioned in the docs as follows :

This is a restricted capability. On first use, the system will prompt the user to allow access. Access is configurable in Settings > Privacy

File system. If you submit an app to the Store that declares this capability, you will need to supply additional descriptions of why your app needs this capability, and how it intends to use it. This capability works for APIs in the Windows.Storage namespace

Muhammad Touseef
  • 4,357
  • 4
  • 31
  • 75
  • Thanks. So there's no way to, say, run a `Process.Start()` on a particular folder when working with `UWP` applications? – Samaursa Oct 05 '18 at 00:27
  • I am not aware of what process.start is, what is your use case here? – Muhammad Touseef Oct 05 '18 at 01:22
  • You can use `Launcher.LaunchFolderAsync` to open a folder in file explorer – Martin Zikmund Oct 05 '18 at 03:34
  • @touseefbsb: sorry I was a bit vague. I meant something like this https://stackoverflow.com/a/181857/368599 (or something as simple as `Directory.Exists("D:/my_dir");` – Samaursa Oct 05 '18 at 14:11
  • @MartinZikmund: I need to run a process in a particular folder. I have a library that already does that. Problem is when I call the functions of the library (that otherwise work) from a UWP application, I get an access denied exception. – Samaursa Oct 05 '18 at 14:13
  • The thing is you cannot work with classic File IO classes. You will need to use StorageFolder and StorageFile instead – Martin Zikmund Oct 05 '18 at 14:30
  • I m sure there is a way with Windows.Storage as well to do exactly the same, u may use "storagefolder.getfolderfrompathasync(path)" and if check if the folder sucefully returns means it exists. – Muhammad Touseef Oct 05 '18 at 16:45
  • @touseefbsb: right, however that was just an example of the functionality I would like to run. I have an (existing) library that runs system commands (which is why I mentioned `Process.Start()`) for various operations, including `git`. Regardless, it turns out the only way to do that would be to launch a process with the `FullTrustProcessLauncher`. However, that requires a redesign of the existing library. – Samaursa Oct 05 '18 at 17:44
0

UWP applications have limited access and the regular IO commands do not work, including System.Diagnostic.Process. The only way to launch a process is to use FullTrustProcessLauncher Class as documented here: https://learn.microsoft.com/en-us/uwp/api/Windows.ApplicationModel.FullTrustProcessLauncher.

Working examples can be found here: https://github.com/StefanWickDev/UWP-FullTrust

However, this does not solve the problem of working with existing libraries since they are not processes in the first place. We now have an RPC service where the requests are made from the service and results obtained.

Samaursa
  • 16,527
  • 21
  • 89
  • 160