关于理论资料可查看:
 
 
public static Encoding GetFileEncoding(string filePath)
                {
                        Encoding encoding = null;
                        FileStream fStream = File.Open(filePath, FileMode.Open);
                        if (fStream.CanSeek)
                        {
                                byte[] data = new byte[4];
                                fStream.Read(data, 0, 4);

                                int markerLength = 0;
                                switch (data[0])
                                {
                                        case 0xef: // UTF8
                                                markerLength = 3;
                                                if (data.Length < 3)
                                                        break;
                                                if (data[1] == 0xbb && data[2] == 0xbf)
                                                        encoding = Encoding.UTF8;
                                                break;
                                        case 0xfe: // UTF 16 BE
                                                markerLength = 2;
                                                if (data[1] == 0xff)
                                                        encoding = Encoding.BigEndianUnicode;
                                                break;
                                        case 0xff: // UTF 16 LE
                                                markerLength = 2;
                                                if (data[1] == 0xfe)
                                                        encoding = Encoding.Unicode;
                                                break;
                                        default:
                                                encoding = Encoding.ASCII;
                                                break;
                                }
                        }

                        fStream.Dispose();
                        fStream.Close();
                        fStream = null;
                        return encoding;
                }