5

Possible Duplicate:
Determine a string's encoding in C#

Many text editorsr (like Notepad++) can detect encoding of arbitrary file. Can I detect encodoing of file in C#?

2
  • Have you searched the web for examples of encoding detection in c#? Commented Sep 19, 2010 at 16:39
  • duplicate of stackoverflow.com/questions/1025332/… Commented Sep 19, 2010 at 16:40

1 Answer 1

9

A StreamReader will try to automatically detect the encoding of a file if there's a BOM when trying to read:

public class Program
{
    static void Main(string[] args)
    {
        using (var reader = new StreamReader("foo.txt"))
        {
            // Make sure you read from the file or it won't be able
            // to guess the encoding
            var file = reader.ReadToEnd();
            Console.WriteLine(reader.CurrentEncoding);
        }
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

+1, though its worth adding that this is not foolproof; many encodings "look" the same to the simple detection method used. Even the best (which is used by the likes of google that can afford to do a lot of crunching and has lots of data to compare streams with) that will consider different possible meanings of "high" octets, aren't 100% perfect. If at all possible, it's best to convey this information precisely.
It works for common encodings, but not for all encodings.
StreamReader does NOT attempt to detect the encoding, it simply uses the default. See the very documentation you linked, where it says: "The default character encoding and default buffer size are used."
The MSDN documentation does say that the default character encoding will be used, but I've tried passing different BOMs to a StreamReader, and it correctly identified them (i.e. reader.CurrentEncoding returned the expected encoding). I tested with UTF-8, UTF-16-BE and UTF-16LE. Note @Darin's comment though - it won't work if you don't read some data.
reader.Peek() is enough
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.