The code will demonstrate how to use the Save File Dialog Box to Save a File
private void btnSaveTextFile_Click(object sender, System.EventArgs e)
{
StreamWriter sw = null;
try
{
sdlgTextFile.AddExtension = true;
// Check to verify that the output path
// actually exists. Prompt before
// creating a new file? Prompt before
// overwriting?
// The default is true.
sdlgTextFile.CheckPathExists = true;
// The default is false.
sdlgTextFile.CreatePrompt = false;
// The default is true.
sdlgTextFile.OverwritePrompt = true;
// The default is true.
sdlgTextFile.ValidateNames = true;
// The default is false.
sdlgTextFile.ShowHelp = true;
// if the user doesn't supply an extension,
// and if the AddExtension property is
// true, use this extension.
// The default is "".
sdlgTextFile.DefaultExt = "txt";
// Prompt with the current file name
// if you've specified it.
// The default is "".
sdlgTextFile.FileName = FileName;
// The default is "".
sdlgTextFile.Filter = "Text files (*.txt)|*.txt|" + "All files|*.*";
sdlgTextFile.FilterIndex = 1;
if (sdlgTextFile.ShowDialog() == DialogResult.OK)
{
FileName = sdlgTextFile.FileName;
sw = new StreamWriter(FileName);
sw.Write(txtFileContents.Text);
}
}
catch (Exception exp)
{
MessageBox.Show(exp.Message, this.Text);
}
finally
{
if (sw != null)
{
sw.Close();
}
}
}