File Picker Control In Windows 10 UWP

FilePicker represents a control that allows a user to pick a file.

Step 1: Create a Blank Windows 10 Universal App (C#) and enter App Name.

  
 
Step 2:

Next add one Button and two Textblock controls for displaying information from the toolbox then change the button name to SelectAPicture and TextBlock2 Name: OutputTextBlock.
 
 
 
Step 3: Add the following namespace.
  1. using System.Threading.Tasks;  
  2. using Windows.Storage.Pickers;  
  3. using   Windows.Storage;  

Copy and paste the following code to the SelectAPicture Button event in the cs page.

  1. private async void SelectAPicture _Click(object sender, RoutedEventArgs e)    
  2. {  
  3.             FileOpenPicker openPicker = new FileOpenPicker();  
  4.             openPicker.ViewMode = PickerViewMode.Thumbnail;  
  5.             openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;  
  6.             openPicker.FileTypeFilter.Add(".jpg");  
  7.             openPicker.FileTypeFilter.Add(".jpeg");  
  8.             openPicker.FileTypeFilter.Add(".png");  
  9.             StorageFile file = await openPicker.PickSingleFileAsync();  
  10.             if (file != null)  
  11.             {  
  12.                   
  13.                 OutputTextBlock.Text = "Selected Photo:" + file.Name;  
  14.             }  
  15.             else  
  16.             {  
  17.                 OutputTextBlock.Text = "Operation cancelled.";  
  18.             }   
  19. }  
Step 4: Run your application and select a picture, It will be shown in our OutputTextBlock.

 
Read more articles on Window 10:

Up Next
    Ebook Download
    View all
    Learn
    View all