2

Possible Duplicate:
How can I detect the encoding/codepage of a text file

I have to read a (text) .txt file.But I don't know its encoding type.The encoding type may be

Encoding.ANSI 

or

Encoding.Unicode

So my question is 'How can we know and read that a specific file is of particular encoding type?'

Community
  • 1
  • 1
doesdos
  • 1,751
  • 4
  • 14
  • 13

2 Answers2

2

try this

StreamReader sr = new StreamReader(@"C:\CreateAsciiFile.txt",true);
string LineText= sr.ReadLine();
System.Text.Encoding enc = sr.CurrentEncoding;

Check this answer it might help you : http://www.eggheadcafe.com/community/aspnet/2/10077748/get-file-encoding.aspx

have look to this also : Encoding Methods

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
1

In general - you can't. The StreamReader constructor has an builtin heuristic to guess the encoding if you don't provide it, which may be good enough for you, but that is not a 100% solution

Read this article of Joel Spolsky

http://www.joelonsoftware.com/articles/Unicode.html

to get more insights.

Doc Brown
  • 19,739
  • 7
  • 52
  • 88