Summary of garbled problems in C # StreamReader reading Chinese

Source: Internet
Author: User

there was a previous article "C # Read and write configuration files tutorial" (http://blog.csdn.net/lisenyang/article/details/47291083) One of the problems is that when reading the configuration file in Chinese garbled situation. What is this for? The reason is that the default encoding is Unicode when the file is processed by the operating system since Windows 2000. So. NET file is also Unicode for the default encoding. Unless otherwise specified, the default encoding for StreamReader is Unicode, not the ANSI code page for the current system. However, most of the documents are stored in ANSI encoding, Chinese text is GB2312, so it is garbled in Chinese.
first of all, get to know a thing with you first--- "System's current ANSI code page encoding" , if you are dealing with ANSI and codepage files that are GB2312, or I/O interfaces of other known encoding types, it is best to write encoding.getencoding ("GB2312") directly, Because Encoding.default may change, and if it is a completely new system, it is best to use "Encoding.UTF8" because UTF8 is compatible with most languages in the world and is more efficient. NET default is also with UTF8 encoding type!

Let's look at the ANSI code page encoding for viewing and modifying the current system:

Linux System View locale method: In terminal input localewindows View code page method: Cmd.exe input chcp, or right-click Cmd.exe property View. Modify Locale:linux System Modify Profile/etc/sysconfig/i18n or/etc/sysconfig/languagewindows at Start-Control panel-Regional and Language Options-Select a language to modify session Local method: Windows on the cmd command line input mode con CP select=437linux at Terminal Export LANG=ZH_CN. UTF8



Before my code is as follows:




The modified code is as follows:




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Summary of garbled problems in C # StreamReader reading Chinese

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.