SoFunction
Updated on 2025-03-01

C# reads and writes text files in specified encoding format

You often read and write text files during work. When reading files, you need to judge the file format by the first two bytes, and then read the contents in the file according to that format.
When writing files, you must also write them in the format specified by the target file. Only in this way can the subsequent environment be read correctly.

1 View format
Open a file in the vs2010 development environment, and then from the menu, File--Advanced Save option, you can see the encoding format of the current file.
For example, the file sees [Simplified Chinese (GB2312)-Code Page 936], which is GB2312.
What you see in the file is [Unicode (UTF-8 with signature)-code page 65001], which is UTF-8.
Commonly used formats are: ASCII, UTF-8, UTF-7, UTF-32, Unicode, GB2312.

2 The format file is

 Encoding encode=("GB2312"));
 You can use the attached class,Read the encoding format of the file first
 encode = ("in_file.txt");
 string strStr1 = ("", encode);

3 Write the format file as

 StreamWriter sw = new StreamWriter("", false, ("ASCII"));
 ("12.3");
 ();

4 Read and write the complete code of the file according to the encoding format of the file

using System;
using ;
using ;
using ;
using ;
using ;
using ;

namespace WebApplication1
{
//=1=Read and write according to file encoding format  public partial class _Default : 
  {
    string proj_name = "";
    protected void Page_Load(object sender, EventArgs e)
    {
      string xml_in_file="c:\\";//Input the fragment, other programs generate a node      string xml_out_file="c:\\";// Overall, new node fragments should be added to their tails//1 Read the encoding format of the input file and read all the text according to its encoding      Encoding encode1 = (xml_in_file);
      
      StringBuilder strSb1 = new StringBuilder();
      string strStr1 = (xml_in_file, encode1);
//
      StringBuilder strSb = new StringBuilder();
      ();

//2 Read the encoding format of the output file and read all text according to its encoding      Encoding encode6 = (xml_out_file);
      ("{0} \r\n", (xml_out_file, encode6));
      (strStr1, "");//Replace the old paragraph with empty//New node fragment, replace the overall end tag, that is, add it to the end      ("</object_set>", strStr1 + "\r\n" + "</object_set>");//New insert to the end
      // FileInfo myFile = new FileInfo(xml_out_file);
      // StreamWriter sw = ();
      StreamWriter sw = new StreamWriter(xml_out_file, false, encode6);//("GB2312"));
      (());
      ();
    }
  }


//=2=Get the file encoding format class  public class fileEncode
  {//Get the file encoding format class    public static  GetFileEncodeType(string filename)
    {
       fs = new (filename, , );
       br = new (fs);
      Byte[] buffer = (2);
      ();
      ();

      if (buffer[0] >= 0xEF)
      {
        if (buffer[0] == 0xEF && buffer[1] == 0xBB)
        {
          return .UTF8;
        }
        else if (buffer[0] == 0xFE && buffer[1] == 0xFF)
        {
          return ;
        }
        else if (buffer[0] == 0xFF && buffer[1] == 0xFE)
        {
          return ;

        }
        else
        {
          return ;
        }
      }
      else
      {
        return ;
      }
    }

  }
}

The above is how to read and write text files in C# with specified encoding format. I hope it will be helpful to everyone's learning.