'File.ReadAllText can't read
My problem is that I want to parse a file and it had to detect a special char ('Â') to do some stuff. I didn't manage to detect it but it worked with normal chars like 'a'. So I tried to understand where the problem was coming from, and created a .txt file whose only char is 'Â'.
string a = File.ReadAllText("C:/example/example/test.txt");
Console.WriteLine(a.Length);`
Console prints 0. It's like the char doesn't exist. So I tried with different encodings (File.ReadAllText with utf8, utf16, unicode and so) and had the same result.
I really don't know what to do, thanks in advance!
Solution 1:[1]
I changed the project target framework and this error message appeared. Try to add "System.IO" before File.ReadAllLines
e.g. Instead of:
File.ReadAllLines(text);
Use:
System.IO.File.ReadAllLines(text);
It happened when you are using two different library with the same function. In this case you need to define the proper library as well.
Solution 2:[2]
It works, if you set the encoding as Default:
string result = File.ReadAllText("test.txt", Encoding.Default);
This will give you the "Â".
Solution 3:[3]
You are trying to read Latin character(s) which is 8859-1 encoding. Try below
Encoding iso = Encoding.GetEncoding("ISO-8859-1");
string a = File.ReadAllText("C:/example/example/test.txt",iso);
Console.WriteLine(a.Length);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | moh99 |
| Solution 2 | Simão Ferreira |
| Solution 3 | Prany |
