SIGN UP MEMBER LOGIN:    
ARTICLE

WPF OpenFileDialog

Posted by Raj Kumar Articles | WPF with C# January 28, 2010
This article will tell you how to use OpenFileDialog using WPF. OpenFileDialog represents a common dialog box that displays the control that allows the user to open a file.
Reader Level:

This article will tell you how to use OpenFileDialog using WPF. OpenFileDialog represents a common dialog box that displays the control that allows the user to open a file. This class cannot be inherited.

First of all make a new WPF application and go to .xaml window.

<Grid>
        <Button Content="Browse" Height="32" HorizontalAlignment="Left"
                Margin="20,12,0,0" Click="button1_Click"
                Name="button1" VerticalAlignment="Top" Width
="136" />
        <Image Margin="12,70,0,20" Name="MyImage" Stretch="Fill" HorizontalAlignment="Left" Width="200" />
        <ListBox Margin="263,70,46,20" Name="listBox1" />
    </Grid>

.xaml.cs

private void button1_Click(object sender, RoutedEventArgs e)
        {
            Stream checkStream = null;
            Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();

            openFileDialog.Multiselect = false;
            //
openFileDialog.InitialDirectory = "c:\\";
            //if you want filter only .txt file
            //dlg.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";

            //if you want filter all files           
            openFileDialog.Filter = "All Image Files | *.*";

            if ((bool)openFileDialog.ShowDialog())
            {
               
try
                {
                    if ((checkStream = openFileDialog.OpenFile()) != null)
                    { 
                       MyImage.Source = new BitmapImage(new Uri(openFileDialog.FileName, UriKind.Absolute));
                        listBox1.Items.Add(openFileDialog.FileName);
                        MessageBox.Show("Successfully done");
                    }
                }
                 catch (Exception ex)
                {
                        MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }

            }
           
else
            {

                MessageBox.Show("Problem occured, try again later");

             }
        }

Now run application. You will see result like this.

1.jpg

Figure 1.

When you click on Browse button that wil open your given directory name, if you does't give InitialDirectory name then that will open Pictures directory.

2.jpg

Figure2.

3.jpg

Figure3.
 

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • 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
    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
Nevron Gauge for SharePoint
Become a Sponsor