C # file writing methods

Source: Internet
Author: User

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

  1. // Instantiate a save file dialog box
  2. SaveFileDialog sf =NewSaveFileDialog ();
  3. // Set the file storage type
  4. Sf. Filter = "txt file | *. txt | all files | *.*";
  5. // If the user does not enter the extension, the suffix is automatically appended.
  6. Sf. AddExtension =True;
  7. // Set the title
  8. Sf. Title = "Writing Files ";
  9. // If the user clicks the Save button
  10. If(Sf. ShowDialog () = DialogResult. OK)
  11. {
  12. // Instantiate a file stream ---> associated with the written file
  13. FileStream fs =NewFileStream (sf. FileName, FileMode. Create );
  14. // Obtain the byte array
  15. Byte[] Data =NewUTF8Encoding (). GetBytes (This. TextBox1.Text );
  16. // Start writing
  17. Fs. Write (data, 0, data. Length );
  18. // Clear the buffer and close the stream
  19. Fs. Flush ();
  20. Fs. Close ();
  21.  
  22. }

C # file Writing Method 2: Using StreamWriter

  1. // Instantiate a save file dialog box
  2. SaveFileDialog sf =NewSaveFileDialog ();
  3. // Set the file storage type
  4. Sf. Filter = "txt file | *. txt | all files | *.*";
  5. // If the user does not enter the extension, the suffix is automatically appended.
  6. Sf. AddExtension =True;
  7. // Set the title
  8. Sf. Title = "Writing Files ";
  9. // If the user clicks the Save button
  10. If(Sf. ShowDialog () = DialogResult. OK)
  11. {
  12. // Instantiate a file stream ---> associated with the written file
  13. FileStream fs =NewFileStream (sf. FileName, FileMode. Create );
  14. // Instantiate a StreamWriter --> associated with fs
  15. StreamWriter sw =NewStreamWriter (fs );
  16. // Start writing
  17. Sw. Write (This. TextBox1.Text );
  18. // Clear the buffer
  19. Sw. Flush ();
  20. // Close the stream
  21. Sw. Close ();
  22. Fs. Close ();
  23. }
  24.  

C # file Writing Method 3: BinaryWriter

  1. // Instantiate a save file dialog box
  2. SaveFileDialog sf =NewSaveFileDialog ();
  3. // Set the file storage type
  4. Sf. Filter = "txt file | *. txt | all files | *.*";
  5. // If the user does not enter the extension, the suffix is automatically appended.
  6. Sf. AddExtension =True;
  7. // Set the title
  8. Sf. Title = "Writing Files ";
  9. // If the user clicks the Save button
  10. If(Sf. ShowDialog () = DialogResult. OK)
  11. {
  12. // Instantiate a file stream ---> associated with the written file
  13. FileStream fs =
  14. NewFileStream (sf. FileName, FileMode. Create );
  15. // Instantiate BinaryWriter
  16. BinaryWriter bw =NewBinaryWriter (fs );
  17. Bw. Write (This. TextBox1.Text );
  18. // Clear the buffer
  19. Bw. Flush ();
  20. // Close the stream
  21. Bw. Close ();
  22. Fs. Close ();
  23. }

 

 

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 );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.