SaveFileDialog prompts users when saving files. This control in the Windows Forms framework is an ideal way for you to allow the user to set a file name for a specific file. Then you can use the event handling mechanism to add custom code to write the file that the user wants to save.
This C# tutorial shows how to use the SaveFileDialog control in Windows Forms.


To begin, you should create a new Windows Forms Application in Visual Studio. This tutorial requires that you add two separate controls to the blank Windows Form. In the Toolbox pane, double-click on the Button and also the SaveFileDialog icons. This will insert them into the form.
In this tutorial, the Button control you just added will be used to open the SaveFileDialog. Like any dialog, you must call the ShowDialog method to open your SaveFileDialog. To add a click event handler to the SaveFileDialog, please double-click on the button in the designer window. Also, double-click on the SaveFileDialog icon in your Visual Studio designer window as well to add the FileOk event handler.
Program that uses SaveFileDialog [C#]
using System;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;
namespace WindowsFormsApplication30
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// When user clicks button, show the dialog.
saveFileDialog1.ShowDialog();
}
private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
{
// Get file name.
string name = saveFileDialog1.FileName;
// Write to the file name selected.
// ... You can write the text from a TextBox instead of a string literal.
File.WriteAllText(name, "test");
}
}
}
In this code, you will see that the button1_Click event handler was added, and the saveFileDialog1_FileOk event handler was added. In the button1_Click method, we simply call the ShowDialog method on the saveFileDialog1 instance. This will make the save file dialog appear on the user's screen when he or she presses the button.
In the saveFileDialog1_FileOk event handler, we handle the user pressing the OK button. At this point, the user wants to actually save the file to the disk. So in this method, we can do actually anything we want, but typically you will want to read the FileName property from the saveFileDialog1 instance. Then, you can use any file writing method you want to actually output any data to that location. In this example, I simply write a string literal "test" to a file; in a real program, you could read a property such as textBox1.Text and write the string returned by that.
TextBoxThere are lots of properties on the SaveFileDialog control type in Windows Forms. The AddExtension, CheckFileExists, CheckPathExists, CreatePrompt, DefaultExt, Filter, FilterIndex, OverwritePrompt, RestoreDirectory, ShowHelp, SupportMultiDottedExtensions, and ValidateNames properties are fairly self-explanatory. For more details about these, please consult the more detailed MSDN documentation from the smart people at Microsoft.
MSDN reference

When developing interactive Windows Forms programs where you want the ability for a user to specify a file name to save to, the SaveFileDialog is useful. This tutorial gave you a walkthrough of how you can add a Button to open the SaveFileDialog, and then actually write to the selected path when the user clicks to save. And although the tutorial does not provide specifics for more detailed tasks, it gives you a sampling of how to approach the process.
Windows Forms