.netcore c#代码中使用如下代码尝试获取指定的字符编码时会抛出异常。

System.Text.Encoding.GetEncoding("GB2312")

//或者

System.Text.Encoding.GetEncoding("GBK")

获取中文字符编码,如GB2312、GBK时会抛出异常:
Unhandled Exception: System.ArgumentException: 'GB2312' is not a supported encoding name. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method.
或者
Unhandled Exception: System.ArgumentException: 'GBK' is not a supported encoding name. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method.
原因是在.netcore中没有安装注册GB2312或者GBK编码

解决方法

  • Nuet中安装System.Text.Encoding.CodePages包
  • 注册编码,在代码中加入如下代码
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
最后修改:2022 年 09 月 30 日
如果觉得我的文章对你有用,请随意赞赏