Posted by: Sean | June 15, 2010

C# – OpenFileDialog

Prelude: After finding that I was not using my blog at all I decided it was time for a change.  So going forward I’m going to be using my blog to post little pieces of code that I find useful, usually pieces that I can never remember how to implement without having to search for.  I’m sure from time to time I’ll still post some ramblings, but essentially this blog is going full nerd.

C# – OpenFileDialog

The OpenFileDialog, as you may have already guessed, is used to display the dialog for opening files in DotNet.  There are basically two ways to use it, the first is through the control, the second is through the object instance.

Control:

Pretty straight forward, just drag the control onto your form.

image

Once that is done the control will appear in the bottom control bar (in VS).

image

From there you can set all of the properties for the control in the Properties window.

image

Note: You can find a complete listing of the properties here.

Since all of the properties are set in the control, there is no need to set them in the code (although you still can of course).  Implementation from there is simple.

if (openFileDialog1.ShowDialog() == DialogResult.OK)
    MessageBox.Show(openFileDialog1.FileName);

The ShowDialog() method is what does the work and actually displays the Windows dialog, to which you’re looking for a return value of DialogResult.Ok.

That’s more or less it for the control, pretty simple.

Object:

Using the OpenFileDialog as an object only really requires one more line of code, the creation of the object itself.

OpenFileDialog openFileDialog = new OpenFileDialog();

However, since there is no associated properties window we need to set the object properties in the code.

openFileDialog.InitialDirectory = @"C:\Users\Sean Thomas\Documents\";
openFileDialog.Filter = "PDF files (*.pdf)|*.txt|All files (*.*)|*.*";
openFileDialog.FilterIndex = 2;

The InitialDirectory and Filter are probably the two most widely used properties for this class. InitialDirectory is pretty straight forward, just sets the directory displayed when the ShowDialog() method is called. The Filter

property on the other hand is used to control the file filter dropdown on the dialog. The basic form for the property is <Display Text>|<Extension>. Each extension set is separated with a pipe.

Note: You can find a complete listing of the properties here.

From there the implementation is virtually the same as the control.

if (openFileDialog.ShowDialog() == DialogResult.OK)
    MessageBox.Show(openFileDialog.FileName);

Again, you’re looking for the return value of DialogResult.Ok.

Summary:

The OpenFileDialog class is a very simple, yet powerful class with many ways to customize it for use.  Similar to the OpenFileDialog class is it’s sister class; OpenFolderDialog (I’ll assume you can guess what it’s for).

For the complete write-up on the class check the MSDN documentation here.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Categories

Follow

Get every new post delivered to your Inbox.