Is there a way to open file explorer and select a file from a UWP application?

I can open the file explorer from UWP applications using Launcher.LaunchFolderAsync() ( + ), but is there a way to make the file selected in this file explorer window?

There are several ways to achieve this in Win32 applications that are directly related to calling explorer.exe and pass parameters to it, which obviously will not work for UWP.

+5
source share
1 answer

You can also use Launcher.LaunchFolderAsync and use the second Folder​Launcher​Options parameter.

Folder​Launcher​Options can make the file or folder that you want to select using ItemsToSelect .

ItemsToSelect is a ItemsToSelect -only property, but you can add items to an existing list.

Here is an example, getting the folder using FolderPicker , and then selecting all the files:

The first is to get the folder:

  FolderPicker p = new FolderPicker(); p.FileTypeFilter.Add(".txt"); StorageFolder folder = await p.PickSingleFolderAsync(); 

And then get all the files in the folder

  foreach (var temp in await folder.GetFilesAsync()) 

I can use FolderLauncherOptions to add the item I want to select.

  var t = new FolderLauncherOptions(); foreach (var temp in await folder.GetFilesAsync()) { t.ItemsToSelect.Add(temp); } 

Then open file explorer

  await Launcher.LaunchFolderAsync(folder, t); 

You can see that Explorer will open when all files are selected.

You can also add folders to ItemsToSelect and it will be selected.

See here https://docs.microsoft.com/en-us/uwp/api/Windows.System.Launcher#Windows_System_Launcher_LaunchFolderAsync_Windows_Storage_IStorageFolder_Windows_System_FolderLauncherOptions_

+5
source

Source: https://habr.com/ru/post/1267227/


All Articles