Cause: Unknown Encoding
Solution:
StreamReader sr = new StreamReader (filename, System. Text. Encoding. Default)
// Add the red part to indicate that the encoding method is the same as that of the system (Chinese), and filename indicates the file path.
StreamWriter sw = new StreamWriter (filename, false, System. Text. Encoding. Default)
I use a UTF-8.
The reason is that since Windows 2000 and later operating systems use Unicode by default during file processing, the default file encoding for. Net is also Unicode. Unless otherwise specified, StreamReader is encoded as Unicode by default, rather than the ANSI code page of the current system. However, most documents are still stored in ANSI encoding, and Chinese text uses gb2312, which causes Chinese garbled characters. That is to say, you must specify the encoding format when reading the text.
The solution is System. Text. Encoding. Default.
StreamReader can be encoded in the current operating system.
StreamReader reader = new StreamReader (FileName, System. Text. Encoding. Default)