C# 判断文件编码
2021-04-14 11:26
标签:ati str style utf8 encoding col text var code 无耐网上各种方法都有缺陷,此方法为原创,暂问发现问题。如发现请指正 C# 判断文件编码 标签:ati str style utf8 encoding col text var code 原文地址:https://www.cnblogs.com/nanfei/p/8963277.htmlpublic static Encoding GetFileEncodingByContent(string path)
{
var contentWithUTF8 = File.ReadAllText(path, Encoding.UTF8);
var contentWithGBK = File.ReadAllText(path, Encoding.GetEncoding("GBK"));
if (contentWithUTF8.Length contentWithGBK.Length)
return Encoding.UTF8;
else if (contentWithUTF8.Length == contentWithGBK.Length)
{
using (var reader = new StreamReader(path, true))
{
reader.Peek(); // you need this!
return reader.CurrentEncoding;
}
}
else
return Encoding.GetEncoding("GBK");
}
上一篇:Delphi 操作 Word