SaveFileDialog In C#

A SaveFileDialog control is used to save a file using Windows SaveFileDialog. A typical SaveFileDialog looks like Figure 1 where you can see the Windows Explorer type features to navigate through folders and save a file in a folder.

save a file in a folder

Figure 1

Creating a SaveFileDialog

We can create a SaveFileDialog control using a Forms designer at design-time or using the SaveFileDialog class in code at run-time (also known as dynamically). Unlike other Windows Forms controls, a SaveFileDialog does not have and not need visual properties like others.

Note
Even though you can create a SaveFileDialog at design-time it is easier to create a SaveFileDialog at run-time.

Design-time

To create a SaveFileDialog control at design-time, you simply drag and drop a SaveFileDialog control from Toolbox to a Form in Visual Studio. After you drag and drop a SaveFileDialog on a Form, the SaveFileDialog looks like Figure 2.

SaveFileDialog

Figure 2

Adding a SaveFileDialog to a Form adds the following two lines of code.

private System.Windows.Forms.SaveFileDialog saveFileDialog1;  
this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();  

Run-time

Creating a SaveFileDialog control at run-time is merely a work of creating an instance of SaveFileDialog class, setting its properties and adding SaveFileDialog class to the Form controls.

The first step to create a dynamic SaveFileDialog is to create an instance of SaveFileDialog class. The following code snippet creates a SaveFileDialog control object.

SaveFileDialog SaveFileDialog1 = new SaveFileDialog();  

ShowDialog method displays the SaveFileDialog.

SaveFileDialog1.ShowDialog(); 

Once the ShowDialog method is called, you can browse and select a file.

Setting SaveFileDialog Properties

After you place a SaveFileDialog control on a Form, the next step is to set properties.

The easiest way to set properties is from the Properties Window. You can open Properties window by pressing F4 or right clicking on a control and selecting Properties menu item. The Properties window looks like Figure 3.

SaveFileDialog Properties window

Figure 3

Initial and Restore Directories

InitialDirectory property represents the directory to be displayed when the open file dialog appears the first time.

SaveFileDialog1.InitialDirectory = @"C:\";  

If RestoreDirectory property is set to true that means the open file dialog box restores the current directory before closing.

SaveFileDialog1.RestoreDirectory = true;  

Title

Title property is used to set or get the title of the open file dialog.

SaveFileDialog1.Title = "Browse Text Files";  

Default Extension

DefaultExtn property represents the default file name extension.

SaveFileDialog1.DefaultExt = "txt";  

Filter and Filter Index

Filter property represents the filter on an open file dialog that is used to filter the type of files to be loaded during the browse option in an open file dialog. For example, if you need users to restrict to image files only, we can set Filter property to load image files only.

SaveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";  

FilterIndex property represents the index of the filter currently selected in the file dialog box.

SaveFileDialog1.FilterIndex = 2; 

Check File Exists and Check Path Exists

CheckFileExists property indicates whether the dialog box displays a warning if the user specifies a file name that does not exist. CheckPathExists property indicates whether the dialog box displays a warning if the user specifies a path that does not exist.

SaveFileDialog1.CheckFileExists = true;  
SaveFileDialog1.CheckPathExists = true; 

File Name and File Names

FileName property represents the file name selected in the open file dialog.

textBox1.Text = SaveFileDialog1.FileName; 

If MultiSelect property is set to true that means the open file dialog box allows multiple file selection. The FileNames property represents all the files selected in the selection.

this.SaveFileDialog1.Multiselect = true;    
foreach(String file in SaveFileDialog1.FileNames) {    
    MessageBox.Show(file);    
}

SaveFileDialog Example

The following code snippet is the code for Save button click event handler. Once a text file is selected, the name of the text file is displayed in the TextBox.

private void SaveButton_Click(object sender, EventArgs e) {      
    SaveFileDialog saveFileDialog1 = new SaveFileDialog();      
    saveFileDialog1.InitialDirectory = @ "C:\";      
    saveFileDialog1.Title = "Save text Files";      
    saveFileDialog1.CheckFileExists = true;      
    saveFileDialog1.CheckPathExists = true;      
    saveFileDialog1.DefaultExt = "txt";      
    saveFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";      
    saveFileDialog1.FilterIndex = 2;      
    saveFileDialog1.RestoreDirectory = true;      
    if (saveFileDialog1.ShowDialog() == DialogResult.OK) {      
        textBox1.Text = saveFileDialog1.FileName;      
    }      
}

Summary

A SaveFileDialog control allows users to launch Windows Save File Dialog and let them save files. In this article, we discussed how to use a Windows Save File Dialog and set its properties in a Windows Forms application.


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.