public enum EncodingType
{
WindowsANSI = 1252,
Unicode = 1200,
UnixANSI = 28591
}
You can pass the path of the file and this method returns the encoding format
of type which has been defined in enum EncodingType.You just modify the enum with your encoding and code page value and get the encoding format of any file.
private Encoding DetectEncoding(string fileName)
{
byte[] data = new byte[4];
Encoding encoding = null;
StreamReader streamReader = new StreamReader(fileName);
streamReader.BaseStream.Read(data, 0, data.Length);
ApplicationNativeMethods.IsTextUnicodeFlags isTextUnicodeFlags = ApplicationNativeMethods.IsTextUnicodeFlags.UnicodeMask;
bool isTextUnicode = ApplicationNativeMethods.IsTextUnicode(data, 4, ref isTextUnicodeFlags);
if ((data[0] == 0xFF && data[1] == 0xFE) || (isTextUnicode == true))
{
encoding = Encoding.GetEncoding((int)EncodingType.Unicode);
}
else
{
streamReader.BaseStream.Position = 0;
char cr = char.MinValue;
char lf = char.MinValue;
char peekLf = char.MinValue;
while ((streamReader.EndOfStream != true))
{
peekLf = (char)streamReader.Peek();
if (peekLf == Constants.SYMBOL_LINEFEED)
{
cr = lf;
lf = (char)streamReader.Read();
break;
}
lf = (char)streamReader.Read();
}
if (cr != Constants.SYMBOL_CARRIAGERETURN)
{
encoding = Encoding.GetEncoding((int)EncodingType.UnixANSI);
}
else
{
encoding = Encoding.GetEncoding((int)EncodingType.WindowsANSI);
}
}
streamReader.Close();
return encoding;
}
0 comments:
Post a Comment