I have a textfile with the content:
A B C D Ä 1 4 0 $ % & € / [ ) = ß ² µ §
If you ask me about the encoding – I have no idea. If I open it with Notepad++ I see in the encoding menu Encoding in ANSI
I would like to read this file, and recognize every character correctly. As code I have this:
//open and locking the file
using (FileStream fs = File.Open(@"C:\testfile.txt", FileMode.Open, FileAccess.Read, FileShare.None))
{
using (TextReader reader = new StreamReader(fs))
{
string line;
//reading and printing each line
while ((line = reader.ReadLine()) != null)
{
System.Console.WriteLine(line);
}
}
}
As output I get:

So for Ä € ß ² µ § I get a ?. That why I thought It’s because of the console, so changed it to UTF8, so I’m maybe able to get a better output. But its not really helping.
System.Console.OutputEncoding = System.Text.Encoding.UTF8;

Thats why I think there is something wrong while reading the file. I should probably change the encoding of the StreamReader. But there are not that many options. I was trying UTF8, ASCII, but it’s not helping. Any ideas?
Edit: Thanks Matthew, adding System.Text.Encoding.Default to the StreamReader is helping. Now only the char € is not recognizable. Don’t get it, are some chars “special”?
Edit2: alright, the € was only a problem because the console is buggy(?). If I look at the string in the debug mode, the € is also fine.
So the working solution for me is now:
1.) Using the reader with default encoding:
using (TextReader reader = new StreamReader(fs, System.Text.Encoding.Default))
AND
2.) Not using the console for output, just reading the string in debug mode
If you are using ANSI, you can do it like this:
However, that will only work if your current code page is correct for the file that you are reading. It probably will be, but for full portability you should determine the actual code page that you’re using and use:
where codePageNumber is the code page of the text file.