c# - Encoding.GetEncoding 无法在 UWP 应用程序中工作

标签 c# encoding win-universal-app uwp

我需要为本地字符编码一些文本文件。

在我的 Windows 8.1 商店应用程序中,我可以正常使用 Encoding.GetEncoding() 方法:

Encoding.GetEncoding("windows-1254")

但是在 UWP 应用程序中,我遇到了这个异常:

Additional information: 'windows-1254' is not a supported encoding name. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method.

我不知道如何使用 Encoding.RegisterProvider 方法让它工作,有什么想法吗?

最佳答案

我们需要使用 CodePagesEncodingProvider 来注册包含在该特定提供程序中的扩展编码。参见 CodePagesEncodingProvider

Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
Encoding.GetEncoding("windows-1254");

引用 https://msdn.microsoft.com/en-us/library/system.text.encodingprovider(v=vs.110).aspx

The .NET Framework Class Library provides one static property, P:System.Text.CodePagesEncodingProvider.Instance, that returns an EncodingProvider object that makes the full set of encodings available on the desktop .NET Framework Class Library available to .NET Core applications.

MSDN 论坛中的相关主题:Encoding.RegisterProvider -- How to use?

关于c# - Encoding.GetEncoding 无法在 UWP 应用程序中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33579661/

相关文章:

sql-server - SQL Server 2008 - 将 XML 声明添加到 XML 输出

c# - UWP/C# 应用程序如何登录 XBox 用户并显示玩家代号?

c# - 使用 mvc 2010 在 SQL Server 中上传/下载 pdf 文件

c# - 在 C# 中创建复数类

c# - SerialPort.ReadLine() 返回垃圾

来自API的奇怪字符串的java char编码

c# - 使用 Nodatime 将 DateTime 从特定时区转换为 UTC?

php - 如何将utf8_general_ci编码的数据显示为json?

c# - 具有持续集成的多语言应用程序工具包

c# - 无法在 WindowsUniversal 应用程序中使用 WebView 控件打开 .html 文件