Open File Using FileOpenPicker In Windows Store App

The FileOpenPicker is used to select and open files in a Windows Store app. In this article, I will demonstrate how to use the FileOpenPicker class to browse, select and view files.

Step 1.

Create a Windows Store App using Visual Studio 2013.

Step 2.

Double-click on the MainPage.xaml to create your UI.

Add a Button, a TextBlock and an Image control to the page. Change the Name properties of these controls. For example, I changed the Name property of the Button control to FileNameButton and the Image control Name property to ImageViewer.

The final UI looks like Figure 1.

 

Figure 1.

Step 3.

We will be using some files defined in the following namespace.

Hit F7 while you are on MainPage.xaml in the designer.

Let's add the following namespaces after other namespace definitions at the top of the MainPage's code behind file.

 

  1. using Windows.Storage.Pickers;  
  2. using Windows.Storage;
  3. using Windows.UI.Xaml.Media.Imaging;  

 

Step 4.

Now let's create a button click event handler. Select Button control and open the Properties Windows and find the Click property and double-click on it. It will add the click event handler to the code behind.

Now one more thing I will change is to add “Async” to the click event handler. The final definition looks like the following:

 

  1. private async void OpenFileButton_Click(object sender, RoutedEventArgs e)  

Now, let's use the FileOpenPicker to browse and open a file. The complete code is listed in Listing 1. The code is very self-explanatory. I set the starting location and file filters then open a stream that is later used to create a BitmapImage.

 

  1. private async void OpenFileButton_Click(object sender, RoutedEventArgs e)   
  2. {    
  3. // Create FileOpenPicker instance    
  4. FileOpenPicker fileOpenPicker = new FileOpenPicker();    
  5.   
  6. // Set SuggestedStartLocation    
  7. fileOpenPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;    
  8.   
  9. // Set ViewMode    
  10. fileOpenPicker.ViewMode = PickerViewMode.Thumbnail;    
  11.   
  12. // Filter for file types. For example, if you want to open text files,  
  13. // you will add .txt to the list.  
  14.   
  15. fileOpenPicker.FileTypeFilter.Clear();    
  16. fileOpenPicker.FileTypeFilter.Add(".png");    
  17. fileOpenPicker.FileTypeFilter.Add(".jpeg");    
  18. fileOpenPicker.FileTypeFilter.Add(".jpg");    
  19. fileOpenPicker.FileTypeFilter.Add(".bmp");  
  20.   
  21. // Open FileOpenPicker    
  22. StorageFile file = await fileOpenPicker.PickSingleFileAsync();    
  23. if (file != null)    
  24. {    
  25. FileNameTextBlock.Text = file.Name;    
  26. // Open a stream    
  27. Windows.Storage.Streams.IRandomAccessStream fileStream =    
  28. await file.OpenAsync(FileAccessMode.Read);  
  29.   
  30. // Create a BitmapImage and Set stream as source    
  31. BitmapImage bitmapImage = new BitmapImage();    
  32. bitmapImage.SetSource(fileStream);  
  33.   
  34. // Set BitmapImage Source    
  35. ImageViewer.Source = bitmapImage;    
  36. }    
  37. }  

 

Step 5.

If hit F5 to run your app. Click on the Open File button and you can see the images in the defined folder are displayed. Select an image and click the Open button.

The output looks like Figure 2.


Figure 2.

Up Next
    Ebook Download
    View all
    Learn
    View all