SIGN UP MEMBER LOGIN:    
ARTICLE

OpenFileDialog in WPF

Posted by Mahesh Chand Articles | WPF August 11, 2009
This article shows how to use an OpenFileDialog control in WPF and C# to browse files.
Reader Level:
Download Files:
 



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.jpg

 

 

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;

 }

share this article :
post comment
 

Be aware that the Microsoft.Win32.OpenFileDialog shows a different dialog that the System.Windows.Forms.OpenFileDialog.

Posted by bt Aug 19, 2010

I guess it depends on what you are doing on mouse down event handler. It may be conflicting with the open dialog.

I have tested with menu event and it works just fine. I have attached updated sample as an article extension.

Posted by Mahesh Chand Oct 13, 2009

i think the problem is the owner-parameter in the showDialog() method
my code looks like this: <UserControl x:Class="DiplomArbeit.Menu"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local = "clr-namespace:DiplomArbeit"
    Height="30" Width="600" x:Name="MenuElement">
    <Menu VerticalContentAlignment="Center" VerticalAlignment="Center" Name="Hauptmenu">
        <MenuItem Header="Datei">
            <MenuItem Header="_öffnen" MouseDown="MenuItem_MouseDown_Datei_öffenen">
              <!--<MenuItem Header="Untermenüs"/>-->
            </MenuItem>
           
           
            <MenuItem Header="_schließen" MouseDown="MenuItem_MouseDown"/>

            <MenuItem Header="_speichern" IsCheckable="true"/>
        </MenuItem>
    </Menu>
</UserControl>

MainWindow:

<Window x:Class="DiplomArbeit.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:uc="clr-namespace:DiplomArbeit"
    Title="Diplomarbeit ReCoTech" WindowStyle="ThreeDBorderWindow" WindowState="Maximized" Height="352" Width="804" Name="GUI">
   
   
    <Grid Background="WhiteSmoke" ShowGridLines="False" Width="auto" Height="auto" VerticalAlignment="Stretch">
       
        <Grid.RowDefinitions>
            <RowDefinition Height="30" />
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <uc:Menu Grid.Row="0" Grid.Column="0"  Width="auto"/>
       

        <uc:Schablone Grid.Row="1" Grid.Column="0"/>
    </Grid>
</Window>

Menu.xaml.cs:

namespace DiplomArbeit
{
    /// <summary>
    /// Interaktionslogik für Menu.xaml
    /// </summary>
    public partial class Menu : UserControl
    {  
        public Menu ()
        {
            InitializeComponent();
        }


        private void MenuItem_MouseDown_Datei_öffenen (object sender, MouseButtonEventArgs e)
        {
            // in this method i want to create the  OpenFileDialog
            OpenFileDialog ofd = new OpenFileDialog();                                                                         ofd.showDialog(i think the mainWindow);
        }

    }
}

Posted by Stephan Malewschik Oct 05, 2009

hi mahesh, thx for the nice description of how to open a file dialog
it's simple and easy to use normally

my problem is that i cant open a file dialog out of a menu item of a menu, that has an MouseDown event which calls a method that creates an OpenFileDialog-object

Posted by Stephan Malewschik Oct 05, 2009
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor