How show/select a file in File Explorer using code?

杨岑 166 Reputation points
2024-05-09T14:25:28.1466667+00:00

Suppose I have a ListView that displays some files. When the user select one, I want to reveal the file in File Explorer. Is this possible in UWP?

Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. Junjie Zhu - MSFT 15,751 Reputation points Microsoft Vendor
    2024-05-10T06:30:12.3233333+00:00

    Hi @杨岑

    Is this possible in UWP?

    Yes, this is possible.

    It needs StorageFile.GetParentAsync and Launcher.LaunchFolderAsync. But you need to add the restricted broadFileSystemAccess capability for your app.

    After the application is deployed, open the Settings > Privacy&security > File system, find the app and turn on the file system access.

     Windows.Storage.StorageFile file;
     private async void Button_Click_Show(object sender, RoutedEventArgs e)
     {
         StorageFolder folder = await file.GetParentAsync();
         await Launcher.LaunchFolderAsync(folder);
     }
     private async void Button_Click_Select(object sender, RoutedEventArgs e)
     {
         var picker = new Windows.Storage.Pickers.FileOpenPicker();
         picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
         picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary;
         picker.FileTypeFilter.Add(".jpg");
         picker.FileTypeFilter.Add(".jpeg");
         picker.FileTypeFilter.Add(".png");
        file = await picker.PickSingleFileAsync();
         if (file != null)
         {         
             Debug.WriteLine("Picked photo: " + file.Name);
         }
         else
         {
             Debug.WriteLine("Operation cancelled.");
             
         }
     }
    

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful