public static Encoding DetectFileEncoding(string fileName)
{
byte[] buf = new byte[12000];
int length;
using (FileStream fstream = File.OpenRead(fileName))
{
length = fstream.Read(buf, 0, buf.Length);
}
Ude.CharsetDetector d = new Ude.CharsetDetector();
d.Feed(buf, 0, length);
d.DataEnd();
return Encoding.GetEncoding(d.Charset);
}