Home > Articles > Programming > User Interface (UI)

This chapter is from the book

Using the OpenFileDialog and SaveFileDialog Controls

A common task for an application is to open or save a file that represents its application data. In Windows applications the Open and Save file dialog boxes provide an easy way for the user to locate a file. The .NET Compact Framework provides the OpenFileDialog and SaveFileDialog controls to interact with these dialog boxes. On Windows CE and Pocket PC, these controls allow you to navigate only the My Documents folder and one folder underneath. Because of this, you may want to create a folder underneath My Documents that will be the default location for application data.

There are a few interesting properties that you may consider setting before displaying one of the file dialog boxes to the user. First, set the Filter property, which determines valid file types and their associated file extensions to be listed in either file dialog box. For example, the following string would allow only files with the .dll or .exe extensions to be listed in the dialog box:

"Dynamically Linked Library|*.dll|Executable|*.exe"

Each file filter will be added to the Files Type drop-down menu in the Open or Save dialog box. You can change the currently selected file filter by setting the FilterIndex property. The default value of this property is 1 because the index is not zero-based.

Finally, the InitalDirectory property determines a folder under My Documents that will initially be enumerated by the dialog boxes. If this property is not set or the folder does not exist, then My Documents is enumerated.

To display an Open or Save file dialog box, call the ShowDialog method on either the OpenFileDialog or SaveFileDialog control. This method will block until the user clicks either the OK button or the Cancel button. ShowDialog returns a DialogResult enumeration value. The DialogResult enumeration has several members, but the ShowDialog method will only return either DialogResult.OK or DialogResult.Cancel. If DialogResult.OK is returned, the user has clicked the OK button, and you can check the Filename property to retrieve the complete path of the file the user selected. If DialogResult.Cancel is returned, the user clicked the Cancel button to discard the dialog.

These controls have different visual representations depending on the OS they are running on. On Pocket PC the dialog boxes will appear full screen. On Windows CE a dialog box will appear as a pop-up dialog window above the application, similar to its desktop counterpart.

The following code demonstrates how to display an OpenFileDialog control that lists files with the .dll or .exe extension. If a file is selected, then a message box appears with the complete filename.

OpenFileDialog ofDlg = new OpenFileDialog();
ofDlg.Filter = "DLL|*.dll|Executable|*.exe";
ofDlg.IntialDirectory = "\\My Documents";

if(DialogResult.OK == ofDlg.ShowDialog()) {
 MessageBox.Show("You Selected " + ofDlg.FileName);
} else {
 MessageBox.Show("Go ahead, select a file!");
Dim ofDlg as OpenFileDialog 
ofDlg = new OpenFileDialog()
ofDlg.Filter = "DLL|*.dll|Executable|*.exe"
ofDlg.IntialDirectory = "\My Documents"

Dim res As DialogResult
If DialogResult.OK = res Then
 MessageBox.Show("You Selected " & ofDlg.FileName)
 MessageBox.Show("Go ahead, select a file!")
End If
  • + Share This
  • 🔖 Save To Your Account