0% found this document useful (0 votes)
130 views2 pages

Open File Dialog in WPF

The document explains how to use the OpenFileDialog class in WPF to browse and select text files. It provides a code example that creates an OpenFileDialog, sets its properties, and updates a TextBox with the selected file name. Additionally, it includes a brief description of the UI components involved, such as a TextBox and a Button for browsing files.

Uploaded by

Abhi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
130 views2 pages

Open File Dialog in WPF

The document explains how to use the OpenFileDialog class in WPF to browse and select text files. It provides a code example that creates an OpenFileDialog, sets its properties, and updates a TextBox with the selected file name. Additionally, it includes a brief description of the UI components involved, such as a TextBox and a Button for browsing files.

Uploaded by

Abhi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

The OpenFileDialog is used to browse files on a machine. The OpenFileDialog class defined in Microsoft.Win32.

OpenFileDialog namespace represents an OpenFileDialog control in WPF. Let's add a TextBox and a Button control to a XAML page. The window looks like this.

OpenFileDialog in WPF

When you click the Browse button, we will browse text files and set the TextBox.Text to the selected file name. <TextBox Height="32" HorizontalAlignment="Left" Margin="6,10,0,0" Name="FileNameTextBox" VerticalAlignment="Top" Width="393" /> <Button Content="Browse" Height="32" HorizontalAlignment="Left" Margin="405,10,0,0" Name="button1" VerticalAlignment="Top" Width="88" Click="button1_Click" /> The code listed in Listing 1 creates an OpenFileDialog, set its default extension and filter properties and calls ShowDialog method that displays the dialog. Once the OpenFileDialog is displayed, you can use it to browse files. Once file is selected, the FileName property of the OpenFileDialog is set to the selected file name. This code sets the selected file name as a TextBox.Text property. // Create OpenFileDialog Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog(); // Set filter for file extension and default file extension dlg.DefaultExt = ".txt"; dlg.Filter = "Text documents (.txt)|*.txt"; // Display OpenFileDialog by calling ShowDialog method Nullable<bool> result = dlg.ShowDialog(); // Get the selected file name and display in a TextBox if (result == true) { // Open document

string filename = dlg.FileName; FileNameTextBox.Text = filename; }


Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.

You might also like