Encoding.GetEncoding 无法在 UWP 应用中工作 [英] Encoding.GetEncoding can't work in UWP app

查看:29
本文介绍了Encoding.GetEncoding 无法在 UWP 应用中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to encode some text files for native characters.

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

In my Windows 8.1 Store app, I could use Encoding.GetEncoding() method normally:

Encoding.GetEncoding("windows-1254")

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

But in UWP app, I got this exception:

附加信息:windows-1254"不是受支持的编码名称.有关定义自定义编码的信息,请参阅 Encoding.RegisterProvider 方法的文档.

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 方法使其工作,有什么想法吗?

I don't know how to make it work by using Encoding.RegisterProvider method, any ideas?

推荐答案

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

We need to use the CodePagesEncodingProvider to register extended encodings included in that specific provider. See CodePagesEncodingProvider Class

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

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

.NET Framework 类库提供了一个静态属性,P:System.Text.CodePagesEncodingProvider.Instance,返回一个EncodingProvider 对象,使完整的编码集可用在桌面上 .NET Framework 类库可用于 .NET Core应用程序.

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 -- 如何使用?

The related thread in MSDN forum: Encoding.RegisterProvider -- How to use?

这篇关于Encoding.GetEncoding 无法在 UWP 应用中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆