Well, when using IO.File.ReadAllText(path)
or ReadAllText(path, System.Text.Encoding.UTF8)
to read a text file which is saved in ANSI encoding, non-latin characters aren't displayed correctly.
So, I decided to use Encoding.Default
. It worked just fine, but I see recommendations against using it everywhere (like here and here) because it "will only guarantee that all UTF-7 character sets will be read correctly". Also Microsoft
says:
Gets an encoding for the operating system's current ANSI code page.
However, it seems to me that it can recognize a file with any encoding. I tested that on a file that contains Chinese, Japanese, and Arabic characters -the file is saved in utf8 encoding-, and I was able to display the file correctly.
Code used:
Dim loadedText As String = IO.File.ReadAllText(path, System.Text.Encoding.Default)
MessageBox.Show(loadedText, "utf8")
Output:
So my question in points:
- Is there something I'm missing here?
- Why is it not recommended to use
Encoding.Default
when reading a file? I know that a file with ANSI encoding would be displayed incorrectly if the default system encoding/system locale is changed, which is something I don't care about in my current case. But.. - Is there even another way to prevent this from happening?
Side note: Please don't mind me using the c#
tag. Although my code is in VB, any answer with C# code is welcomed.