C# SaveFileDialog

To save a file using Windows SaveFileDialog, a SaveFileDialog control is utilized. Figure 1 depicts a typical SaveFileDialog with Windows Explorer-like functionality for navigating between folders and saving a file in a folder.

Create a SaveFileDialog

At design time, we can use a Forms designer to create a SaveFileDialog control, or at run time, we can use the SaveFileDialog class in code. A SaveFileDialog, unlike other Windows Forms controls, does not have and does not require visual attributes.

Note: Although a SaveFileDialog may be created at design time, it is easier to construct a SaveFileDialog at run time.

Design-time

Drag and drop a SaveFileDialog control from the Toolbox to a Form in Visual Studio to create a SaveFileDialog control at design time. When you drag and drop a SaveFileDialog into a Form, it appears like Figure 2.

The following two lines of code are added to a Form when a SaveFileDialog is added.

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

Run-time

At runtime, building a SaveFileDialog control is as simple as creating an instance of the SaveFileDialog class, configuring its settings, and adding the SaveFileDialog class to the Form controls.

The first step in creating a dynamic SaveFileDialog is to initialize the SaveFileDialog class. The code below generates a SaveFileDialog control object.

SaveFileDialog SaveFileDialog1 = new SaveFileDialog();

The SaveFileDialog is displayed using the ShowDialog method.

SaveFileDialog1.ShowDialog();

You may browse and select a file after using the ShowDialog function.

Setting the SaveFileDialog Properties

After you’ve added a SaveFileDialog control to a Form, the following step is to customize it.

The Properties Window is the simplest way to change properties. By hitting F4 or right-clicking on control and selecting Properties from the context menu, you may enter the Properties panel. Figure 3 shows the Properties window.

Directory Creation and Restoration

The InitialDirectory property represents the directory that will be shown when the open file dialogue initially displays.

SaveFileDialog1.InitialDirectory = @"C:\";

The open file dialogue box restores the current directory before closing if the RestoreDirectory property is set to true.

SaveFileDialog1.RestoreDirectory = true;  

Title

The title attribute is used to set or retrieve the title of the open file dialogue.

SaveFileDialog1.Title = "Browse Text Files";

Default Extension

The default file name extension is represented by the DefaultExtn property.

SaveFileDialog1.DefaultExt = "txt";  

Filter and Filter Index

The Filter property represents the filter on an open file dialogue that is used to filter the kind of files to be loaded during the browse option in an open file dialogue. For example, if you want users to be limited to picture files only, we may specify the Filter property to only load image files.

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

The FilterIndex property is the index of the filter that is currently selected in the file dialogue box.

SaveFileDialog1.FilterIndex = 2;  

Check that the file and path exist

The CheckFileExists parameter defines whether the dialogue box shows a warning if the user enters an invalid file name. The CheckPathExists parameter defines whether the dialogue box shows a warning if the user enters an invalid path.

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

Name of the File and File Names

The file name chosen in the open file dialogue is represented by the FileName property.

textBox1.Text = SaveFileDialog1.FileName;  

If the MultiSelect attribute is set to true, several files may be selected in the open file dialogue box. All of the files in the selection are represented by the FileNames attribute.

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

An Example

The code for the Save button click event handler is shown below. The name of the text file is displayed in the TextBox after it is selected.

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;      
    }      
}

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories