How to: Save a File to a Folder
You can use the built-in SaveFileDialog component to enable users to save a file to a folder. To display a dialog box, you use the ShowDialog method. You can then check whether the user clicked the OK button by using the DialogResult.OK field.
To display the folder browser dialog box
On the File menu, click New Project.
The New Project dialog box appears.
Click Windows Forms Application and then click OK.
Add a RichTextBox control to the form, leaving the default name, RichTextBox1.
Add a Button control to the form, and change the following properties in the Properties window:
Property
Value
Name
saveTextFile
Text
Save As
Add a SaveFileDialog component to the form.
saveFileDialog1 appears in the component tray.
Double-click the button to add the default event handler in the Code Editor.
In the saveTextFile_Click event handler, add the following code to display the Save As dialog box. This code saves the text typed in the RichTextBox control to a text file at the specified location.
saveFileDialog1.Filter = "txt files (*.txt)|*.txt"; if(saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK && saveFileDialog1.FileName.Length > 0) { richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText); }
Press F5 to run the code.
When the form appears, type some text in the rich text box.
Click Save As and then browse to a folder where you want to save the text file.
Specify a name for the text file, and then click OK.
Verify that the text file exists at the specified location.
Close the application.
See Also
Concepts
Using Built-in Dialog Boxes in Your Application
Designing a User Interface in Visual C#