BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Windows Programming
.NET 1.1+

The Open File Dialog Box

The OpenFileDialog class provides one of the .NET framework's common dialog boxes. This standardised dialog box allows users to select a single file or a group of files to be opened.

Allowing Multiple File Selection

If your software should allow the user to select more than one file to open at once, you can use multiple selection mode. To do so, simply set the Boolean Multiselect property to true. To determine which files were selected, you can read the FileNames property, which returns an array of strings. Each holds the full path to one of the files.

To demonstrate, modify the code behind the form, as follows:

private void OpenButton_Click(object sender, EventArgs e)
{
    using (OpenFileDialog ofd = new OpenFileDialog())
    {
        ofd.Multiselect = true;

        if (ofd.ShowDialog() == DialogResult.OK)
        {
            ShowFileDetails(ofd);
        }
    }
}

private void ShowFileDetails(OpenFileDialog ofd)
{
    FileList.Items.Clear();

    foreach (string file in ofd.FileNames)
    {
        FileList.Items.Add(file);
    }
}

Adding Filters

So far, the example dialog box has allowed you to select files of any type. In most applications that create documents you will want to limit the files that can be opened. The usual way to achieve this is to use a limited number of file extensions and filter the files in the dialog box to those extensions. For example, if you wanted to open documents created with Microsoft Word you might filter the list to show only items with the "doc", "docx" or "rtf" extensions. For your own file formats you would want to choose your own extensions, avoiding those used by the more commonly installed applications.

The open file dialog box allows you to apply filters in several ways. For the simplest situations, where you only want to allow opening of files of one type, you can filter using a single extension. You can also widen the filter to show two or more types simultaneously, such as in the doc / docx example. If your application is more complex, you can provide a drop-down list of document types, each with a different set of filters.

To set up your filters, you use the Filter property. This holds a string, which must be formatted correctly so that is can be parsed on loading the dialog box.

For a simple filter that shows just one file type, you provide two pieces of information. The first is a description of the file type, which will be displayed to the user. The second is the filter to apply to the file name. The filter can include wildcard characters. Any files with names that match the filter are displayed; other files are hidden. The two parts of the string are separated with a bar (|).

The following shows a filter for text files. The user will see the text, "Text Files" in the dialog box and all files with the "txt" extension will be displayed for selection.

Text Files|*.txt

When you want to show files that match one of several filters, the filter strings should be separated using semicolons (;). For example, the following filter shows image files with the GIF, JPG or PNG extension.

Images|*.gif;*.jpg;*.png

Finally, if there are several types of document that can be opened, each of which has its own filter string, you construct each in the same manner and separate them with bar characters. The options will be displayed in a drop-down list, allowing the user to select a document type.

The following string could be used by a program that can load text files or images. If the user selects, "Text Files", only files with a "txt" extension will be displayed. To show images of any of the three types, the user could choose the "Images" option.

Text Files|*.txt|Images|*.gif;*.jpg;*.png

To demonstrate, change the click event code as shown as below. Run the program and try selecting from the filter options.

private void OpenButton_Click(object sender, EventArgs e)
{
    using (OpenFileDialog ofd = new OpenFileDialog())
    {
        ofd.Multiselect = true;
        ofd.Filter = "Text Files|*.txt|Images|*.gif;*.jpg;*.png";

        if (ofd.ShowDialog() == DialogResult.OK)
        {
            ShowFileDetails(ofd);
        }
    }
}

You'll see that the dialog box defaults to show files according to the first filter. If you want to change the default, you can set the FilterIndex property. This is an integer that specifies the index of the filter to display first. The index is one-based so to choose the second filter you can set it to 2.

private void OpenButton_Click(object sender, EventArgs e)
{
    using (OpenFileDialog ofd = new OpenFileDialog())
    {
        ofd.Multiselect = true;
        ofd.Filter = "Text Files|*.txt|Images|*.gif;*.jpg;*.png";
        ofd.FilterIndex = 2;

        if (ofd.ShowDialog() == DialogResult.OK)
        {
            ShowFileDetails(ofd);
        }
    }
}
23 March 2014