C # Write File processing operations are involved in many development projects. What is the specific implementation method? Here we will introduce you to the three methods, hoping to inspire you in developing applications.
First, the C # Write File processing operation must first be imported into the namespace: using System. IO;
C # file write Method 1: Use FileStream
- // Instantiate a save file dialog box
- SaveFileDialog sf =NewSaveFileDialog ();
- // Set the file storage type
- Sf. Filter = "txt file | *. txt | all files | *.*";
- // If the user does not enter the extension, the suffix is automatically appended.
- Sf. AddExtension =True;
- // Set the title
- Sf. Title = "Writing Files ";
- // If the user clicks the Save button
- If(Sf. ShowDialog () = DialogResult. OK)
- {
- // Instantiate a file stream ---> associated with the written file
- FileStream fs =NewFileStream (sf. FileName, FileMode. Create );
- // Obtain the byte array
- Byte[] Data =NewUTF8Encoding (). GetBytes (This. TextBox1.Text );
- // Start writing
- Fs. Write (data, 0, data. Length );
- // Clear the buffer and close the stream
- Fs. Flush ();
- Fs. Close ();
-
- }
C # file Writing Method 2: Using StreamWriter
- // Instantiate a save file dialog box
- SaveFileDialog sf =NewSaveFileDialog ();
- // Set the file storage type
- Sf. Filter = "txt file | *. txt | all files | *.*";
- // If the user does not enter the extension, the suffix is automatically appended.
- Sf. AddExtension =True;
- // Set the title
- Sf. Title = "Writing Files ";
- // If the user clicks the Save button
- If(Sf. ShowDialog () = DialogResult. OK)
- {
- // Instantiate a file stream ---> associated with the written file
- FileStream fs =NewFileStream (sf. FileName, FileMode. Create );
- // Instantiate a StreamWriter --> associated with fs
- StreamWriter sw =NewStreamWriter (fs );
- // Start writing
- Sw. Write (This. TextBox1.Text );
- // Clear the buffer
- Sw. Flush ();
- // Close the stream
- Sw. Close ();
- Fs. Close ();
- }
-
C # file Writing Method 3: BinaryWriter
- // Instantiate a save file dialog box
- SaveFileDialog sf =NewSaveFileDialog ();
- // Set the file storage type
- Sf. Filter = "txt file | *. txt | all files | *.*";
- // If the user does not enter the extension, the suffix is automatically appended.
- Sf. AddExtension =True;
- // Set the title
- Sf. Title = "Writing Files ";
- // If the user clicks the Save button
- If(Sf. ShowDialog () = DialogResult. OK)
- {
- // Instantiate a file stream ---> associated with the written file
- FileStream fs =
- NewFileStream (sf. FileName, FileMode. Create );
- // Instantiate BinaryWriter
- BinaryWriter bw =NewBinaryWriter (fs );
- Bw. Write (This. TextBox1.Text );
- // Clear the buffer
- Bw. Flush ();
- // Close the stream
- Bw. Close ();
- Fs. Close ();
- }
In addition, it is also available when adding content at the end of a text file:
System. IO. File. AppendAllText (FileName, contents, Encoding );
For richTextBox, you can load and save files as follows:
RichTextBox1.LoadFile (FileName, RichTextBoxStreamType. PlainText); richTextBox1.SaveFile (FileName, RichTextBoxStreamType. PlainText );