SoFunction
Updated on 2025-03-06

Solution to the garbled code of C# reading Chinese files

This article describes the solution to the garbled code in C# when reading Chinese files. Share it for your reference. The specific analysis is as follows:

Let’s take a look at this code first:

FileStream aFile = new FileStream(SingleFile,);
StreamReader sr = new StreamReader(aFile,("gb2312"),true);
string FileContent = ();
();
ProcessData Pd = new ProcessData();
(FileContent);

StreamReader uses 3 parameters. The last one automatically detects utf-8. Most of the Chinese is gb2312. If it is not utf-8, use gb2312.

The system comes with utf detection, see as follows:

private void DetectEncoding()
{
 if ( >= 2)
 {
 this._detectEncoding = false;
 bool flag = false;
 if (([0] == 0xfe) && ([1] == 0xff))
 {
   = new UnicodeEncoding(true, true);
  (2);
  flag = true;
 }
 else if (([0] == 0xff) && ([1] == 0xfe))
 {
  if ((( < 4) || ([2] != 0)) || ([3] != 0))
  {
  = new UnicodeEncoding(false, true);
 (2);
 flag = true;
  }
  else
  {
  = new UTF32Encoding(false, true);
 (4);
 flag = true;
  }
 }
 else if ((( >= 3) && ([0] == 0xef)) && (([1] == 0xbb) && ([2] == 0xbf)))
 {
   = Encoding.UTF8;
  (3);
  flag = true;
 }
 else if (((( >= 4) && ([0] == 0)) && (([1] == 0) && ([2] == 0xfe))) && ([3] == 0xff))
 {
   = new UTF32Encoding(true, true);
  (4);
  flag = true;
 }
 else if ( == 2)
 {
  this._detectEncoding = true;
 }
 if (flag)
 {
   = ();
  this._maxCharsPerBuffer = ();
   = new char[this._maxCharsPerBuffer];
 }
 }
}

I hope this article will be helpful to everyone's C# programming.