Пример #1
0
        private static void ResolveEncoding(String filePath, out String exportExtensionKeyName, out Encoding encoding)
        {
            using (FileStream fs = File.OpenRead(filePath))
            {
                ICharsetDetector cdet = new CharsetDetector();

                byte[]  buffer = new byte[1024];
                Boolean done   = false;
                int     count;

                while ((count = fs.Read(buffer, 0, buffer.Length)) > 0 && !done)
                {
                    cdet.Feed(buffer, 0, count);
                    done = cdet.IsDone();
                }
                cdet.DataEnd();

                if (String.Equals(cdet.Charset, Ude.Charsets.UTF8, StringComparison.Ordinal))
                {
                    exportExtensionKeyName = Utf8ExportExtensionKeyName;
                    encoding = Encoding.UTF8;
                }
                else if (String.Equals(cdet.Charset, Ude.Charsets.UTF16_LE, StringComparison.Ordinal))
                {
                    exportExtensionKeyName = Utf16ExportExtensionKeyName;
                    encoding = Encoding.Unicode;
                }
                else if (String.Equals(cdet.Charset, Ude.Charsets.UTF32_LE, StringComparison.Ordinal))
                {
                    exportExtensionKeyName = Utf32ExportExtensionKeyName;
                    encoding = Encoding.UTF32;
                }
                else
                {
                    exportExtensionKeyName = OemExportExtensionKeyName;
                    encoding = Encoding.Default;
                }
            }
        }