This article is a mirror article of machine translation, please click here to jump to the original article.

View: 20162|Reply: 2

[Solved] StreamReader solution for garbled characters when reading files

[Copy link]
Posted on 4/13/2018 4:11:20 PM | | |
Today, when I was making an Open File dialog OpenFileDialog, I needed to read the open file and display it in the RichTextBox in the form. However, when displaying the file, it was found that the displayed Chinese string was garbled. The key parts of the code are as follows:
At first, I also thought that there was a problem with the character encoding, so when I initialized StreamReader, I added the character encoding parameters to it, and added it several times, Encoding.Unicode, Encoding.UTF32, Encoding.UTF7, Encoding.UTF8, Encoding.ASCII, I tried all the encoding schemes that could be added, but the result was not good.


Originally, the operating system from Windows 2000 and later used Unicode for file processing, so . The default file encoding for .Net is also Unicode. Unless otherwise specified, the default encoding of StreamReader is Unicode, not the current system's ANSI code page. However, most of the documents are still stored in ANSI encoding, and Chinese text uses gb2312, which causes Chinese garbled characters, which means that the encoding format must be specified when reading the text.


The workaround is: System.Text.Encoding.Default tells the StreamReader the encoding of the current operating system.






Previous:.net/c# Xml, Json, Hessian, Protocol Buffers serialization comparison
Next:.net/c# Use Costura.Fody to package the DLL into the EXE
 Landlord| Posted on 4/13/2018 5:11:39 PM |
This post was last edited by QWERTYU on 2018-4-13 17:18

If it still doesn't work, the server may have compressed the data, taking gzip as an example, you need to decompress the data first.

Posted on 4/19/2018 1:31:02 PM |
If the web content is compressed with gzip and the content is garbled, there are two solutions

First solution:

//
        Summary:
        Get or set the type of decompression used.
        //
        Return results:
        A T:System.Net.DecompressionMethods object that indicates the type of decompression used.
        //
        Anomaly:
        //   T:System.InvalidOperationException:
        The current state of the object does not allow this property to be set.
        public DecompressionMethods AutomaticDecompression { get; set; }
The following line of code is done, the changes are very small!!




Second solution:



Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com