The SaveFileDialog Control
The SaveFileDialog control (System.Windows.Forms.SaveFileDialog) allows you to save or write data to a specified file. The dialog allows you to browse your file system and pick a directory, then type a filename you want to be the name of the file that will contain the data to be written.
You can type the name of the file that exists in the current directory and the dialog can prompt you if you want to overwrite it.
You can also type the whole path of a file or directory in the File Name text box located at the bottom of the dialog.
SaveFileDialog Control In C#
The following table shows some useful properties of the SaveFileDialog control.
Property | Description |
---|---|
AddExtention | Specifies whether to automatically add an extension when the user does not specify a file extension. |
CheckFileExists | Specifies whether to initiate a warning if the user types a file that does not exist. |
CheckPathExists | Specifies whether to initiate a warning if the user types a path that does not exist. |
DefaultExt | The default Extention to add when the user does not indicate a file extension. |
FileName | The file selected by the user. This can also be the default selected file when the dialog shows up. |
Filter | Allows you to add a filter which is a special string indicating which types of files are only allowed to be opened by the user. |
FilterIndex | If multiple filters are present, this indicates which filter shows as the default starting with index 1. |
InitialDirectory | The initial directory that the OpenFileDialog will show. |
OverwritePrompt | Specifies whether the dialog will prompt you to overwrite a file when an existing file is already found. |
RestoreDirectory | Specifies whether to restore to the default directory when the dialog closes. |
Title | The title of the dialog. |
c# save file dialog text file
The AddExtention property automatically adds an extension when the user does not indicate the file extension of the file name.
The extension to add is specified by the DefaultExt property. The CheckFileExists and CheckPathExists methods are recommended to be set to true so the dialog will issue a warning message if it cannot find the specified file or directory. The InitialDirectory property specifies the initial directory that the dialog will show when you open it up.
The Title property is the title of the dialog located at the title bar. The FileName property is the Filename specified by the user.
Read More:-The FontDialog Control
Specifying File Types
We can specify the file types that the file will have. We use the Filter property which accepts a string containing a special pattern. For example, we can set the dialog to only allow our file to save as a text file with .txt file extensions. The Filter property requires a special pattern.
Description1|Extension1|Description2|Extention2|...DescriptionN|ExtentionN]
We first start with a description telling about the type of file. We then follow it with a vertical bar (|) followed by the extension. For example, the following pattern allows you to save files as text files.
Text Files|.txt
The description here is Text Files and the extension is .txt. You can specify multiple extensions. For example, the following pattern allows you to save a file as a .txt, or .png.
Text Files|*.txt|Image|*.png
You can select the type that the file will be saved as using the combo box below the File Name text box.
We will now create an example application that uses the basic capabilities of the SaveFileDialog control. The application will allow a user to type into a multiline text box and then save the text into a text file using a specified file name and path. Please note that we need to import the System.IO namespace in our code
using System.IO;
Create a form similar to the one below. Use a multiline text box by setting the Multiline property to true. Drag a SaveFileDialog control from the Dialogs category of the Toolbox to the form.
Double click the button to create an event handler for its Click event. Again, import the System.IO namespace first at the top of the code. Use the following code for the event handler.
private void button1_Click(object sender, EventArgs e){//Specify the extensions allowedsaveFileDialog1.Filter = "Text File|.txt";//Empty the FileName text box of the dialogsaveFileDialog1.FileName = String.Empty;//Set default extension as .txtsaveFileDialog1.DefaultExt = ".txt";//Open the dialog and determine which button was pressedDialogResult result = saveFileDialog1.ShowDialog();//If the user presses the Save buttonif (result == DialogResult.OK){//Create a file stream using the file nameFileStream fs = new FileStream(saveFileDialog1.FileName, FileMode.Create);//Create a writer that will write to the streamStreamWriter writer = new StreamWriter(fs);//Write the contents of the text box to the streamwriter.Write(textBox1.Text);//Close the writer and the streamwriter.Close();}}
The first line specifies that we can only save our file as a text file with a .txt extension. The second one assigns an empty string to the FileName so the File Name text box of the dialog will be initially empty (you can indicate a default filename though). We set the default extension to be .txt so if the user only types the name and forgot or does not include the extension, the dialog will automatically add the extension name in case no file type is selected.
We then open the SaveFileDialog using its ShowMethod property which returns a DialogResult value. The user can now browse the directory where he/she wants to save the file. When the user presses the Save button, then the method ShowDialog will return DialogResult.OK.
We tested this using an if statement. We created a FileStream object and set the file name and file mode. We then create a StreamWriter object and pass the FileStream object. We use the Write method of the writer to write the contents of the text box to the stream and save it to the file. Finally, we close the writer which also closes the file stream.
Next: C# Color Dialog Box
Execute the application and type anything inside the text box. Click the button and choose a directory. Then type a file name and hit Save. If a similar file exists, you may be prompted if the program should overwrite the file. Setting OverwritePrompt to false will automatically overwrite the file without prompting.
Do you know:-
- How to Retrieve data using dataset in c#
- “Login failed for user” SQLConnection using C#
- How can I prevent the browser back button After Logout
- Calendar Control in c# Windows Application
- Timer tick event in c# windows application
- How to delete record in c# using the connected approach
- [Connect] to Database c# Through Code
- The Command Class | C# | TrickCode
- C# DataReader ADO.NET |Trickcode
- What is disconnected data access?
- how to Adding Parameters to Commands
- SQL Basics?
- What is Database Preparations?
- SQL vs NoSQL or MySQL vs MongoDB
- What is data provider in C#?
- What is connection string C#?
- [SQL] SELECT Statement | Example
- how to get return value from stored procedure in sql server
- how to prevent sql injection attacks
- What is Database? What is SQL?
- C#.Net How To: Send email in asp.net using c#
If you like the tutorial, then please share this tutorial with your friends on social media.
Post A Comment:
0 comments: