在.net core使用GB2312时抛出以下异常
System.ArgumentException:“'gb2312' is not a supported encoding name. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method.
解决方法
在NuGet 添加“System.Text.Encoding.CodePages”
在代码中还需要添加Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
static void Main(string[] args) { Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); byte[] bytes = Encoding.GetEncoding("gb2312").GetBytes("罗分明 www.luofenming.com"); string s = Encoding.GetEncoding("GB2312").GetString(bytes); Console.WriteLine("Hello World!"); }
链接:http://www.luofenming.com/show.aspx?id=ART2020031600001