Search code examples
windows-phone-8encodingstreamreader

Windows phone 8, how StreamReader handle GB encoding text correctly?


I copied a GB2312 encoded text file into device's isolated storage from PC. I have the following code to read it out. The device's language is set to Chinese Simplified. However, the text readed back is all garbage except numerical and alphabets. Does seem the encoding isn't set right. However, I might not dealing with only GB code. What is wrong with the code? Thanks!

            string fileName = "周杰伦-听妈妈的话.lrc";

        using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
        {
            using (StreamReader sr = new StreamReader(store.OpenFile(fileName, FileMode.Open, FileAccess.Read), true))
            {
                while (true)
                {
                    String line = sr.ReadLine();
                    if (line == null)
                        break;
                }
            }
        }

Solution

  • Have you tried using the StreamReader c'tor that contains the correct Encoding for your file?

    public StreamReader(
        Stream stream,
        Encoding encoding
    )
    

    The character encoding is set by the encoding parameter, and the default buffer size is used. The StreamReader object attempts to detect the encoding by looking at the first three bytes of the stream. It automatically recognizes UTF-8, little-endian Unicode, and big-endian Unicode text if the file starts with the appropriate byte order marks. Otherwise, the user-provided encoding is used. See the Encoding.GetPreamble method for more information.