SaveFileDialog In C#

A SaveFileDialog control is used to save a file using Windows Save File Dialog. A typical Save File Dialog looks like Figure 1 where you can see 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.

  1. private System.Windows.Forms.SaveFileDialog saveFileDialog1;  
  2. 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.

  1. SaveFileDialog SaveFileDialog1 = newSaveFileDialog();  

ShowDialog method displays the SaveFileDialog.

  1. 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.

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.

  1. SaveFileDialog1.InitialDirectory = @"C:\";  

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

  1. SaveFileDialog1.RestoreDirectory = true;  

Title

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

  1. SaveFileDialog1.Title = "Browse Text Files";  

Default Extension

DefaultExtn property represents the default file name extension.

  1. 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.

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

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

  1. 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.

  1. SaveFileDialog1.CheckFileExists = true;  
  2. SaveFileDialog1.CheckPathExists = true;  

File Name and File Names

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

  1. 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.

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

Sample 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.

  1. privatevoid SaveButton_Click(object sender, EventArgs e) {  
  2.     SaveFileDialog saveFileDialog1 = newSaveFileDialog();  
  3.     saveFileDialog1.InitialDirectory = @ "C:\";  
  4.     saveFileDialog1.Title = "Save text Files";  
  5.     saveFileDialog1.CheckFileExists = true;  
  6.     saveFileDialog1.CheckPathExists = true;  
  7.     saveFileDialog1.DefaultExt = "txt";  
  8.     saveFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";  
  9.     saveFileDialog1.FilterIndex = 2;  
  10.     saveFileDialog1.RestoreDirectory = true;  
  11.     if (saveFileDialog1.ShowDialog() == DialogResult.OK) {  
  12.         textBox1.Text = saveFileDialog1.FileName;  
  13.     }  
  14. }  

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.