SoFunction
Updated on 2025-03-08

Summary of reading and writing methods for C# implementing text files

Method 1:

using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
 
namespace txt
{
  public partial class Form1 : Form
  {
    // string path;
    public Form1()
    {
      InitializeComponent();
      +=button3_Click;
    }
 
    private void textBox2_TextChanged(object sender, EventArgs e)
    {
      string path1 = ;
      if(!(path1))
      {
        ("The file does not exist");
      }
    }
    //Browse Button    private void button3_Click(object sender, EventArgs e)
    {
      /*if (() == )
      {
 
        string selUrl = ;
      }*/
     
     OpenFileDialog ofd = new OpenFileDialog();
 
      if (() == )
      {
         = ;
      }
 
      //Select a folder      /* FolderBrowserDialog fbd = new FolderBrowserDialog();
      ();
      ();
       */
    }
    //Read the file    private void button1_Click(object sender, EventArgs e)
    {
      if(!=null)
        //Read the file content and display it in textbox1 for the user to modify      {
        string path=;
        if ((path))
       //  {
        //   (path);
        // }
         = (path, );
      }
    }
 
    private void button2_Click(object sender, EventArgs e)
    {
      // string[] appendText=;
       
      (, , );
      ("Save successfully");
    }

  }
}

Method 2:

namespace Open text file to test 
{ 
 public partial class Form1 : Form 
 { 
  public Form1() 
  { 
   InitializeComponent(); 
  } 
  private void btn_Read_Click(object sender, EventArgs e) 
  { 
   //Exception detection begins   try
   { 
    FileStream fs = new FileStream(@tB_PachFileName.Text , , );//Read file settings    StreamReader m_streamReader = new StreamReader(fs, ("GB2312"));//Set the code for reading and writing    //Use StreamReader class to read files    m_streamReader.(0, ); 
    // Read each line from the data stream until the last line of the file and display the content in rTB_Display.Text    this.rTB_Display.Text = ""; 
    string strLine = m_streamReader.ReadLine(); 
    while (strLine != null) 
    { 
     this.rTB_Display.Text += strLine + "\n"; 
     strLine = m_streamReader.ReadLine(); 
    } 
    //Close this StreamReader object    m_streamReader.Close(); 
   } 
   catch
   { 
    //throw an exception    ("The specified file does not exist"); 
    return; 
   } 
   //Exception detection ends  } 
  private void btn_Replace_Click(object sender, EventArgs e) 
  { 
   //Judge the start of replacement   if (tB_Replace.Text == ""&&tB_Replace_2.Text=="") 
   { 
    ("If you don't have any characters you want to replace, you're so talented."); 
   } 
   else
   { 
    if (rTB_Display.Text == "") 
    { 
     ("The file content is empty and cannot be replaced, please check the file"); 
    } 
    else
    { 
     string str = rTB_Display.(); 
     rTB_Display.Text = (@tB_Replace.Text ,@tB_Replace_2.Text);//replace    } 
   } 
   //Finish  } 
  private void btn_Save_Click(object sender, EventArgs e) 
  { 
   //Exception detection begins   try
   { 
    //Create a file stream to write or create a StreamWriter    FileStream fs = new FileStream(@tB_Save.Text, , ); 
    StreamWriter m_streamWriter = new StreamWriter(fs); 
    m_streamWriter.Flush(); 
    // Use StreamWriter to write content in a file    m_streamWriter.(0, ); 
    // Write the contents in richTextBox1 to the file    m_streamWriter.Write(rTB_Display.Text); 
    //Close this file    m_streamWriter.Flush(); 
    m_streamWriter.Close(); 
   } 
   catch
   { 
    //throw an exception    ("Write to the file failed, please check whether the path file name and permissions meet"); 
   } 
   //Exception detection ends  } 
 } 
}

Method 3:

//Write to text file  class WriteTextFile
  {
    static void Main()
    {
      //If the file does not exist, create it; if it exists, overwrite it      //This method writes character array to display new line      string[] lines = { "first line", "second line", "third line","Fourth Line" };
      (@"C:\testDir\", lines, Encoding.UTF8);

      //If the file does not exist, create it; if it exists, overwrite it      string strTest = "This example tests a string to write to a text file.";
      (@"C:\testDir\", strTest, Encoding.UTF8);

      // Process text lines before writing text to file      //StreamWriter one parameter is overridden by default      //The second parameter of StreamWriter is false to overwrite the existing file. If true, the text will be appended to the end of the file.      using ( file = new (@"C:\testDir\",true))
      {
        foreach (string line in lines)
        {
          if (!("second"))
          {
            (line);//Open the end of the file directly without changing the line            (line);// Apply the end of the file directly and change the line          }
        }
      }
    }
  }

//Read text file
  class ReadTextFile
  {
    static void Main()
    {
      //Read the string directly      string text = (@"C:\testDir\");
      (text);

      //Read as a string array by line      string[] lines = (@"C:\testDir\");
      foreach (string line in lines)
      {
        (line);
      }

      //Read text file in streaming mode from beginning to end      //This method will read out the text line by line      using ( sr = new (@"C:\testDir\"))
      {
        string str;
        while ((str = ()) != null)
        {
          (str);
        }
      }
      ();
    }
  }

The above is the entire content of this article, I hope you like it.