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 Save File Dialog Box

The SaveFileDialog class provides the functionality for one of the .NET framework's common dialog boxes. This dialog box provides the ability to users to specify the name and path of a file to be created or updated.

Specifying File Types

The SaveFileDialog class inherits from FileDialog, which is the same base class that is used by OpenFileDialog. The two file dialog boxes therefore share a lot of functionality. Rather than repeat all of the information from the earlier article, I'll mention a couple of the shared properties and provide simple code samples. For more detail, refer to the OpenFileDialog article.

One of the key shared elements is the ability to specify the type of file being saved. You can provide one or more file types, each with a description and an extension. The user can select the desired file type and the extension will be added to the file name automatically. Changing the type also filters the files displayed in the dialog box, making it easier to choose a file to overwrite.

To limit the file types to text files or documents with the .doc extension, set the Filter property for the dialog box by changing the click event code, as follows:

private void SaveButton_Click(object sender, EventArgs e)
{
    using (SaveFileDialog sfd = new SaveFileDialog())
    {
        sfd.Filter = "Text Files|*.txt|Documents|*.doc";
        if (sfd.ShowDialog() == DialogResult.OK)
        {
            SaveFile(sfd);
        }
    }
}

Try running the program again to see the filter options.

NB: The details of the syntax for this property are in the earlier article.

Create and Overwrite Prompts

By default, when the user provides a path for a file that already exists, they are prompted to confirm that the file should be overwritten. If the combination of path and file name does not exist, no prompt is shown. In some cases you might not want this behaviour and may elect to change it with the Boolean CreatePrompt and OverwritePrompt properties.

CreatePrompt is set to false by default. If you change its value to true, a prompt will be shown when the user specifies a file path that does not already exist. Conversely, if you change the OverwritePrompt property's value to false, the prompt to overwrite a file is not shown.

Try updating the code for the Click event as follows to see the prompt behaviour reversed from the default:

private void SaveButton_Click(object sender, EventArgs e)
{
    using (SaveFileDialog sfd = new SaveFileDialog())
    {
        sfd.Filter = "Text Files|*.txt|Documents|*.doc";
        sfd.CreatePrompt = true;
        sfd.OverwritePrompt = false;
        if (sfd.ShowDialog() == DialogResult.OK)
        {
            SaveFile(sfd);
        }
    }
}

Setting the Dialog Box's Title

As with the open file dialog box, you can change the text in the window's title bar using the Title property. This is a simple string property that contains the title to be displayed.

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

private void SaveButton_Click(object sender, EventArgs e)
{
    using (SaveFileDialog sfd = new SaveFileDialog())
    {
        sfd.Filter = "Text Files|*.txt|Documents|*.doc";
        sfd.CreatePrompt = true;
        sfd.OverwritePrompt = false;
        sfd.Title = "Save Text Document";
        if (sfd.ShowDialog() == DialogResult.OK)
        {
            SaveFile(sfd);
        }
    }
}
6 May 2014