Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

windows-10

uwp

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.

like image 705
Mahdi Ghiasi Avatar asked Apr 27 '17 11:04

Mahdi Ghiasi


People also ask

How do I access UWP files?

There are two primary ways to access files and folders from your app's data locations: Use ApplicationData properties to retrieve an app data folder. using Windows. Storage; StorageFolder localFolder = ApplicationData.

How do I select Open with File Explorer?

By default, File Explorer opens to Quick access. If you'd rather have File Explorer open to This PC, go to the View tab and then select Options. In the Open File Explorer to: list, select This PC, and then select Apply.

What is open file picker?

The picker uses a single, unified interface to let the user pick files and folders from the file system or from other apps. Files picked from other apps are like files from the file system: they are returned as StorageFile objects. In general, your app can operate on them in the same ways as other objects.

Where is UWP app data stored?

UWP app state is stored in the local part of the user profile exclusively.


1 Answers

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://docs.microsoft.com/en-us/uwp/api/Windows.System.Launcher#Windows_System_Launcher_LaunchFolderAsync_Windows_Storage_IStorageFolder_Windows_System_FolderLauncherOptions_

like image 96
lindexi Avatar answered Sep 20 '22 11:09

lindexi