Skip to main content
 首页 » 编程设计

c#之.NET中文CultureInfo "zh"不存在

2024年02月06日12kuangbin

我正在使用 .NET 3.5 并运行以下代码:

var culture = new CultureInfo("zh"); // Throws ArgumentException 

我从 xml:lang 属性中得到字符串“zh”。我不明白为什么会抛出异常,因为例如“de”(“de-DE”的父级)工作正常。文化“zh-CN”确实有效,但那不是我可以使用的。

编辑: 它适用于 .NET 4.5.1(感谢 xanatos)——因此即使 MSDN 页面解释说中文是一个异常(exception),但在较新的 .NET 版本中行为是不同的。

请您参考如下方法:

CultureInfo上有个小纸条页:

There are two culture names that contradict this rule. The cultures Chinese (Simplified), named zh-Hans, and Chinese (Traditional), named zh-Hant, are neutral cultures. The culture names represent the current standard and should be used unless you have a reason for using the older names zh-CHS and zh-CHT.

所以你可以试试 zh-Hans 或 zh-Hant

和/或您可以看到 What cultures are supported by the CultureInfo class in .NET 3.5?

接受的答案是:

Unfortunately, it is OS dependent. Check here for default language support per OS.