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.

Setting the Dialog Box's Title

When you open the dialog box, the title bar normally shows the text, "Open". Sometimes it is useful to modify this title to give the user further information. This is particularly useful if your software includes several "Open" commands. Visual Studio is an example of such a program. In these cases, you can set the dialog box's title by setting the Title property to an alternative string.

The following updated method changes the title of the dialog box:

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;
        ofd.Title = "Pick some files to process";

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

Folder Selection

When the user selects a file using the open file dialog box, the current folder changes to the selected directory when they click the OK button. The next time that the dialog box is used, the last folder will be displayed. This is good for usability in many situations. Sometimes you will want to set the initial folder. You can do this by specifying a path in the InitialDirectory property.

The following updated method specifies that the dialog box starts showing the "c:\windows" folder:

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;
        ofd.Title = "Pick some files to process";
        ofd.InitialDirectory = @"c:\windows";

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

Detecting Invalid Paths and Files

As well as allowing the user to browse folders and select files using the visible controls, it is also possible to type the name of a file or folder. If the entered file or folder does not exist, a message box is displayed, showing the error.

You can disable these messages with the CheckFileExists and CheckPathExists properties. Both hold Boolean values that default to true. You might decide to set them to false if your program provides the option to either open or create a file. Once the user clicks OK, you could check for the existence of the file before opening it. If it does not exist, you could create and open a new file.

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;
        ofd.Title = "Pick some files to process";
        ofd.InitialDirectory = @"c:\windows";
        ofd.CheckFileExists = false;
        ofd.CheckPathExists = false;

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

Read Only Option

When your application works with networked files, you might want to include the option to open a file in read-only mode, allowing other users to edit it. You would check whether the option was selected when determining how to open the file. To permit this, the OpenFileDialog class includes two Boolean properties. Setting the ShowReadOnly property to true adds an "Open as read-only" checkbox to the dialog box. The ReadOnlyChecked property can be set before showing the dialog to specify the default option for the checkbox. After the dialog box is closed, this same property holds the user's selection.

The following, updated code for the button's click event shows the option and defaults it to open files in read-only mode.

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;
        ofd.Title = "Pick some files to process";
        ofd.InitialDirectory = @"c:\windows";
        ofd.CheckFileExists = false;
        ofd.CheckPathExists = false;
        ofd.ShowReadOnly = true;
        ofd.ReadOnlyChecked = true;

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