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 a selected file name. Figure 1 is an example of an open file dialog.
Figure 1
The OpenFileDialog class is 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, and 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 the 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.
<Window x:Class="FileEncryptionApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:FileEncryptionApp"
mc:Ignorable="d"
Title="File Encryption App" Height="450" Width="800">
<Grid Margin="0,0,0,45.667">
<TextBox HorizontalAlignment="Left" Height="43" Margin="30,10,0,0" TextWrapping="Wrap"
Text="TextBox" VerticalAlignment="Top" Width="436" Name="FileNameTextBox"/>
<Button x:Name="BrowseButton" Content="Browse a file" HorizontalAlignment="Left"
Margin="485,13,0,0" VerticalAlignment="Top" Width="121" Click="BrowseButton_Click"
RenderTransformOrigin="1.047,0.821" Height="40"/>
<TextBlock HorizontalAlignment="Left" Height="282" Margin="30,96,0,0"
TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top"
Width="703" Name="TextBlock1"/>
</Grid>
</Window>
Listing 1
ShowDialog
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.
private void BrowseButton_Click(object sender, RoutedEventArgs e)
{
// Create OpenFileDialog
Microsoft.Win32.OpenFileDialog openFileDlg = new Microsoft.Win32.OpenFileDialog();
// Launch OpenFileDialog by calling ShowDialog method
Nullable<bool> result = openFileDlg.ShowDialog();
// Get the selected file name and display in a TextBox.
// Load content of file in a TextBlock
if (result == true)
{
FileNameTextBox.Text = openFileDlg.FileName;
TextBlock1.Text = System.IO.File.ReadAllText(openFileDlg.FileName);
}
}
Listing 2
As you can see from Listing 2, the code uses the selected file to read its content and displays it in a TextBlock.
The final application looks like Figure 3 which allows you to browse a file and load 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.
// Set filter for file extension and default file extension
openFileDlg.DefaultExt = ".txt";
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.
// Set initial directory
openFileDlg.InitialDirectory = @"C:\Temp\";
Select Multiple Files
You can also select multiple files using the dialog by setting OpenFileDialog's Multiselect property to true.
// Multiple selection with all file types
openFileDlg.Multiselect = true;
openFileDlg.Filter = "All files (*.*)|*.*";
Summary
In this article, we saw how to use Win32 OpenFileDialog in WPF to provide Windows OpenFileDialog functionality.