SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"; saveFileDialog1.Title = "Save a text file"; saveFileDialog1.ShowDialog(); if (saveFileDialog1.FileName != "") { // Save the file using (Stream stream = saveFileDialog1.OpenFile()) using (StreamWriter writer = new StreamWriter(stream)) { writer.WriteLine("Hello, world!"); } }
SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.Filter = "JPEG Image|*.jpg|PNG Image|*.png"; saveFileDialog1.Title = "Save an image file"; saveFileDialog1.ShowDialog(); if (saveFileDialog1.FileName != "") { // Save the image pictureBox1.Image.Save(saveFileDialog1.FileName, ImageFormat.Jpeg); }In this example, the code creates a SaveFileDialog object and sets the filter to allow for JPEG and PNG images. When the user selects a file location and clicks "Save", the code checks that a file name was actually chosen. If so, it saves the image displayed in a PictureBox to the selected file in JPEG format. The package library for SaveFileDialog is part of the .NET Framework, which is included in Microsoft Visual Studio.