用此库:c# 读取文本文件编码格式使用 ude, https://github.com/errepi/ude,可以使用nuget 安装此包,搜索“ude”即可
使用示例
string filePath = "";
//filePath = @"C:\Users\Admin\Downloads\testEncoding\GB2312.txt";
//filePath = @"C:\Users\Admin\Downloads\testEncoding\GBK.txt";
// filePath = @"C:\Users\Admin\Downloads\testEncoding\utf-8.txt";
// filePath = @"C:\Users\Admin\Downloads\testEncoding\utf-8-with-bom.txt";
filePath = @"C:\Users\Admin\Downloads\testEncoding\utf-16.txt";
using (FileStream fs = File.OpenRead(filePath))
{
Ude.CharsetDetector cdet = new Ude.CharsetDetector();
cdet.Feed(fs);
cdet.DataEnd();
if (cdet.Charset != null)
{
Debug.WriteLine("Charset: {0}, confidence: {1}",
cdet.Charset, cdet.Confidence);
Encoding encoding = Encoding.GetEncoding(cdet.Charset);
string content = System.IO.File.ReadAllText(filePath, encoding);
Debug.WriteLine(content);
}
else
{
Debug.WriteLine("Detection failed.");
}
}
如果是.net core 使用且需使用GB2312编码格式、GBK编码格式
的话,还需安装一个encoding的包,原文:https://www.cnblogs.com/mermaidLoft/p/6047423.html
1. 引入了System.Text.Encoding.CodePages.dll
2. 在启动的时候,注册EncodingProvider,执行代码如下:
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
在Program.cs文件加入:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
// Register CodePagesEncodingProvider
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}