C# File Save Dialog

In this article, we will learn how to use save file dialog box in windows form in C#.

Let's create windows form.

Step 1
In Visual Studio select "File" = > "New" = > "Project..." then select C# Windows Forms Application then click Ok.

create windows form

Step 2
Let's create a SaveFileDialog then you go "View" = > select "ToolBox" (Crtl+Alt+X) = > then select "All Windows Forms" = > Choose "SaveFileDialog".

savefile dialog
  1. First of all, we'll know about the "Savefile" dialog.

    Save file dialog is mainly used to save any file to memory in c#. We'll discuss properties and functions and events of savefile dialog write down.

    The following code snippet creates a SaveFileDialog control object under btnsave "OnClick" methods.

    SaveFileDialog saveFileDialog1 = new SaveFileDialog();
    

    ShowDialog() method displays the SaveFileDialog

    saveFileDialog1.ShowDialog();
    

    SaveFileDialog Initial directory are used to gets or sets the initial directory displayed by the file dialog box.

    saveFileDialog1.InitialDirectory = @"F:\";
    

    SaveFileDialog RestoreDirectory are used to get or sets a value indicating whether the dialog box restores the current directory before closing.

    saveFileDialog1.RestoreDirectory = true;
    

    SaveFileDialog Filter are used to get or sets the current file name filter string. which determines the choices that appear in the "Save as file type" or "Files of type" box in the dialog box.

    saveFileDialog1.Filter = "txt files (*.txt)|*.txt";
    

    SaveFileDialog FilterIndex are used to get or sets the index of the filter currently selected in the dialog box.

    saveFileDialog1.FilterIndex = 1;
    

    SaveFileDialog Title are used to get or sets the file dialog box title.

    saveFileDialog1.Title = "Save Your File";
    

    SaveFileDialog DefaultExt are used to get or sets the default file extension.

    saveFileDialog1.DefaultExt = "*.txt";
    

    SaveFileDialog CheckFileExist are used to get or sets a value indicating whether the dialog box displays a warning if the user specifies a file name that does not exist.

    if (saveFileDialog1.CheckFileExists == true)
    

    SaveFileDialog CheckPathExist are used to get or sets a value indicating whether the dialog box displays a warning if the user specifies a file path that does not exist.

    if (saveFileDialog1.CheckPathExists == true)
    

    SaveFileDialog FileName are used to get or sets a string containing the file name selected in the file dialog box.

    saveFileDialog1.FileName = "DefaultFile";
    

    SaveFileDialog OpenFile() are used to Opens the file with read/write permission by the user.

    saveFileDialog1.OpenFile();
    

    SaveFileDialog Reset() are used to reset all dialog box option to their default values.

    saveFileDialog1.Reset();


Write a comment


Name Email Message