Tutorial :C# File.ReadallText doing weird things



Question:

What I'm trying do is read all the text in a file and if it contains the word "Share" do a regex. Here is the code:

DirectoryInfo dinfo = new DirectoryInfo(@"C:\Documents and Settings\g\Desktop\123");          FileInfo[] Files = dinfo.GetFiles("*.txt");          foreach (FileInfo filex in Files)          {              string contents = File.ReadAllText(filex.FullName);              string matchingcontants = "Share";              if (contents.Contains(matchingcontants))              {                  string sharename = Regex.Match(contents, @"\+(\S*)(.)(.*)(.)").Groups[3].Value;                  File.AppendAllText(@"C:\sharename.txt", sharename + @"\r\n");              }            }  

When I debug I get... contents = "\r\0\n\0=\0=\0=\0=\0=\0=\0=\0=\0=\0=\0=\0=\0=\0=\0=\0=\0=\0=\0=\0=\0=\0=\0=\0=\0=\0=\0=\0=\0=\0\r\0\n\0+\0S\0h\0a\0r\0e\0 \0\\0\\0j\05\02\0\\0w\0w\0w\0_\0O\0n\0t\

\0S\0h\0a\0r\0e\

Not Share. Any hints? tips or suggestions?


Solution:1

Looks like you've got a file which is saved as UTF-16 (i.e. Encoding.Unicode). Read the file with the right encoding, and all should be well.

Fortunately there's an overload of File.ReadAllText which takes an encoding:

string contents = File.ReadAllText(filex.FullName, Encoding.Unicode);  

Unfortunately, that will then do the wrong thing for files which aren't in UTF-16. While there are heuristic ways of guessing the encoding, ideally you should know the encoding before you open the file.


Solution:2

Looks like it's a Unicode file, and you're trying to read it as plain ASCII.


Solution:3

My guess is that the encoding is not set correctly, you might need to use ReadAllText(String, Encoding) specifying the encoding.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »