C # StreamReader reads Chinese garbled characters

Source: Internet
Author: User

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)

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.