7

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

There are some ways to achieve this in Win32 apps which involve calling explorer.exe directly and passing parameters to it, which obviously won't work for UWP.

Community
  • 1
  • 1
Mahdi Ghiasi
  • 14,873
  • 19
  • 71
  • 119

1 Answers1

10

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

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

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

Here's an example, getting a folder using FolderPicker and then selecting all files:

The first is get the folder:

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

And then get all files in the folder

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

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

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

Then open the file explorer

      await Launcher.LaunchFolderAsync(folder, t);

You can see that the explorer will be opened while selecting all files.

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

See here for more details: https://learn.microsoft.com/en-us/uwp/api/Windows.System.Launcher#Windows_System_Launcher_LaunchFolderAsync_Windows_Storage_IStorageFolder_Windows_System_FolderLauncherOptions_

lindexi
  • 4,182
  • 3
  • 19
  • 65