OpenFileDialog In WPF

Windows OpenFileDiloag dialog box lets users browse files on a computer. The dialog box not only lets you select a file but also allows you to set an initial directory, types of files to browse, and get selected file name. Figure 1 is an example of an open file dialog.
Figure 1. 
The OpenFileDialog class defined in Microsoft.Win32.OpenFileDialog namespace represents an OpenFileDialog control in WPF and C#.
In this article, we will see how to create a WPF application that uses an OpenFileDialog to browse a file, display its name and also its content in a TextBlock. We will also see how to set the initial directory, various filters, and other properties of OpenFileDialog control.
Create a WPF project using Visual Studio and add a TextBox, a Button, and a TextBlock control to page. The final Window looks like Figure 2.

Figure 2. 
When you click the Browse button, we will browse text files and set the selected file name as the text of the TextBox. We will also read the text file content and display it in TextBlock.
The final XAML of the window is listed in Listing 1. 
  1. <Window x:Class="FileEncryptionApp.MainWindow"  
  2.         xmlns=""  
  3.         xmlns:x=""  
  4.         xmlns:d=""  
  5.         xmlns:mc=""  
  6.         xmlns:local="clr-namespace:FileEncryptionApp"  
  7.         mc:Ignorable="d"  
  8.         Title="File Encryption App" Height="450" Width="800" >  
  9.     <Grid Margin="0,0,0,45.667">  
  10.         <TextBox HorizontalAlignment="Left" Height="43" Margin="30,10,0,0" TextWrapping="Wrap"   
  11.                  Text="TextBox" VerticalAlignment="Top" Width="436" Name="FileNameTextBox"/>  
  12.         <Button x:Name="BrowseButton" Content="Browse a file" HorizontalAlignment="Left"   
  13.                 Margin="485,13,0,0" VerticalAlignment="Top" Width="121" Click="BrowseButton_Click"   
  14.                 RenderTransformOrigin="1.047,0.821" Height="40"/>  
  15.         <TextBlock HorizontalAlignment="Left" Height="282" Margin="30,96,0,0"   
  16.                    TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top"  
  17.                    Width="703" Name="TextBlock1"/>  
  19.     </Grid>  
  20. </Window>  
Listing 1.
Now, let's write a button click event handler. You can simply double click on the Button to add its click handler. 
On the button click event handler, we will write code to launch the OpenFileDialog and select a text file. The Button click event handler code is listed in Listing 2.  
  1. private void BrowseButton_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     // Create OpenFileDialog
  4.     Microsoft.Win32.OpenFileDialog openFileDlg = new Microsoft.Win32.OpenFileDialog(); 
  6.     // Launch OpenFileDialog by calling ShowDialog method
  7.     Nullable<bool> result = openFileDlg.ShowDialog();
  8.     // Get the selected file name and display in a TextBox.
  9.     // Load content of file in a TextBlock
  10.     if (result == true)
  11.     {
  12.         FileNameTextBox.Text = openFileDlg.FileName;
  13.         TextBlock1.Text = System.IO.File.ReadAllText(openFileDlg.FileName);
  14.     }
  15. }
Listing 2.
As you can see from Listing 2, the code uses the selected file to read its content and displays in a TextBlock. 
The final application looks like Figure 3 that allows you to browse a file and loads its content in a TextBlock.  
Figure 3. 
Set Filter
The Filter property of OpenFileDialog is used to set what types of files you want the dialog to browse. You can set multiple file types by using the pipe "|" operator as a file extension separator. The following code snippet sets the dialog to browse text files only.
  1. // Set filter for file extension and default file extension  
  2. openFileDlg.DefaultExt = ".txt";  
  3. openFileDlg.Filter = "Text documents (.txt)|*.txt";  
Set Initial Directory 
The InitialDirectory property is used to set the initial directory of the dialog when the dialog is launched.  
  1. // Set initial directory    
  2. openFileDlg.InitialDirectory = @"C:\Temp\";    
Select Multiple Files 
You can also select multple files using the dialog by setting OpenFileDialog's Multiselect property to true. 
  1. // Multiple selection with all file types    
  2. openFileDlg.Multiselect = true;    
  3. openFileDlg.Filter = "All files (*.*)|*.*";        
In this article, we saw how to use Win32 OpenFileDialog in WPF to provide Windows OpenFileDialog functionality.

Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.