SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.InitialDirectory = "C:\\"; saveFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"; saveFileDialog.FilterIndex = 2; saveFileDialog.FileName = "example"; if (saveFileDialog.ShowDialog() == DialogResult.OK) { // Save the file. using (StreamWriter writer = new StreamWriter(saveFileDialog.OpenFile())) { writer.WriteLine("This is an example file."); } }
SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.InitialDirectory = "C:\\"; saveFileDialog.Filter = "Custom files (*.custom)|*.custom|All files (*.*)|*.*"; saveFileDialog.FilterIndex = 1; saveFileDialog.FileName = "example"; if (saveFileDialog.ShowDialog() == DialogResult.OK) { // Save the file. string fileExtension = Path.GetExtension(saveFileDialog.FileName); if (fileExtension != ".custom") { string newFileName = Path.ChangeExtension(saveFileDialog.FileName, ".custom"); File.Move(saveFileDialog.FileName, newFileName); } }This example is similar to the first example, but with two differences: the filter is set to display only files with a custom extension (".custom" in this case), and the code checks to see if the user specified a different file extension before saving the file. If the user specified a different extension, the code renames the file with the custom extension. Both examples use the Microsoft.Win32 SaveFileDialog class to display a dialog box and prompt the user to save a file to the file system. The first example saves a text file, while the second example saves a custom file with a specific extension.