C# Help reading foreign characters using StreamReader

后端 未结 10 1293
别那么骄傲
别那么骄傲 2020-11-29 02:46

I\'m using the code below to read a text file that contains foreign characters, the file is encoded ANSI and looks fine in notepad. The code below doesn\'t work, when the fi

10条回答
  •  执念已碎
    2020-11-29 03:18

    File.OpenText() always uses an UTF-8 StreamReader implicitly. Create your own StreamReader instance instead and specify the desired encoding. like

    using (StreamReader reader =  new StreamReader(@"C:\test.txt", Encoding.Default)
     {
     // ...
     }
    

提交回复
热议问题