Thursday, July 14, 2011

Selecting Files to Open or Save in VB.NET

VB.NET has come a long way from the clunky Common Dialog control of VB6 for implementing a standard dialog that will let you browse to find a file to Open or Save. Here is an example of each:

Browsing to Open a File in VB.NET
      Dim fileDlg As New OpenFileDialog

fileDlg.Filter = "CSV File (*.csv)|*.csv|txt files (*.txt)|*.txt|All files (*.*)|*.*"
fileDlg.DefaultExt = "csv"

' could be omitted to use default, or a specific dir can be given
fileDlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

If fileDlg.ShowDialog() = DialogResult.OK Then

' Do something with the selected filename
Debug.Print(fileDlg.FileName)

Else

' the dialog was cancelled

End If

The OpenFileDialog object is used here, and I have only shown the most commonly used options. The Filter property lets you specify the which File Types will be selectable form the Open File Dialog (in a dropdown list). The value is set to a string that is a pipe (|) delimited list composed to a File Type Description and Filter Pattern. If you have more than one File Type, you can add another pipe, then another Description|Filter Pattern, and so on. If you leave the Filter property empty, then then Open File Dialog will just not have a drop down to select the File Type filter. Here are some examples:

"CSV File (*.csv)|*.csv"
"CSV File (*.csv)|*.csv|txt files (*.txt)|*.txt"
"CSV File (*.csv)|*.csv|txt files (*.txt)|*.txt|All files (*.*)|*.*"

If you are using Filter with more than one File Type specified, you can also use DefaultExt to specify which File Type to use as the default. Just specify the extension as a string (with no dot). This property also will set the default file extension to use if you don't specify it in the filename (if typed in), so this property can also be used without the Filter property being set.

The InitialDirectory property will let you define which directory to open up in. You can set this property to an explicit path (like "c:\MyData") or you can use the special directories that are defined for you, like:
Environment.SpecialFolder.MyDocument
Environment.SpecialFolder.MyComputer
Environment.SpecialFolder.MyPictures
Environment.SpecialFolder.Desktop

There is also a Title property that can be used to change the name of the Open File Dialog. In this example, it is not used and defaults to "Open File".

After the ShowDialog call returns OK, use the FileName property to get the selected filename.

Browsing to Save a File in VB.NET
   Dim fileDlg As New SaveFileDialog

fileDlg.Filter = "CSV File (*.csv)|*.csv|txt files (*.txt)|*.txt|All files (*.*)|*.*"
fileDlg.DefaultExt = "csv"

' InitialDirectory can be omitted to use default, or used to specify an explicit directory
fileDlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

fileDlg.OverwritePrompt = True

If fileDlg.ShowDialog() = DialogResult.OK Then

' Do something with the selected filename
Debug.Print(fileDlg.FileName)

Else

' the dialog was cancelled

End If

Saving a file is similar to opening a file. You use a SaveFileDialog object instead of a OpenFileDialog object. Many of the properties work the same way as with the OpenFileDialog such as Filter, DefaultExt, and InitialDirectory. I also show OverwritePrompt set to True (which is the default). This setting will cause the dialog to prompt you when you try to save over an existing file.

After the ShowDialog call returns OK, use the FileName property to get the filename.

If you want to select a Directory instead of a file, look at this post.

No comments: