简体   繁体   English

使用CultureAndRegionInfoBuilder更新内置的.Net文化

[英]Using CultureAndRegionInfoBuilder to update built-in .Net culture

I'm attempting to change the built in .Net culture fr-CA, using the following method: 我正在尝试使用以下方法更改内置的.Net文化fr-CA:

    CultureAndRegionInfoBuilder cib = new CultureAndRegionInfoBuilder("fr-CA", CultureAndRegionModifiers.Replacement);

    try
    {
        Console.WriteLine("Updating " + cib.CultureName);

        cib.NumberFormat.CurrencyGroupSeparator = ",";
        cib.NumberFormat.CurrencyDecimalSeparator = ".";
        cib.NumberFormat.NumberGroupSeparator = ",";
        cib.NumberFormat.NumberDecimalSeparator = ".";

        cib.Register();

        Console.WriteLine("Culture updated.");
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
        Console.ReadKey();
    }

However, the Register() call fails with "System.InvalidOperationException: The 'Register' method failed because the custom culture 'fr-CA' already exists." 但是,Register()调用失败,并显示“ System.InvalidOperationException:由于自定义区域性'fr-CA'已经存在,'Register'方法失败。”

Is it possible to update the built-in culture? 是否可以更新内置文化? According to the docs ( http://msdn.microsoft.com/en-us/library/system.globalization.cultureandregioninfobuilder.cultureandregioninfobuilder.aspx ), it looks like I can just update it, though I might be reading that wrong. 根据文档( http://msdn.microsoft.com/zh-cn/library/system.globalization.cultureandregioninfobuilder.cultureandregioninfobuilder.aspx ),尽管我可能读错了它,但我似乎只能对其进行更新。

I just had to add: 我只需要添加:

CultureAndRegionInfoBuilder.UnRegister("fr-CA");

before Register(). 在Register()之前。

You may be better to create a copy of an existing culture and Register it with a new name such as "fr-CACustom". 创建现有文化的副本并用新名称(例如“ fr-CACustom”)注册可能会更好。

[Note that a custom culture can be registered on a computer only by a user who has administrative rights on that computer.] [请注意,只有具有对计算机的管理权限的用户才能在计算机上注册自定义区域性。]

try
{
    CultureAndRegionInfoBuilder cib = new CultureAndRegionInfoBuilder("fr-CACustom", CultureAndRegionModifiers.None);
    cib.LoadDataFromCultureInfo(new CultureInfo("fr-CA"));
    Console.WriteLine("Creating" + cib.CultureName);

    cib.NumberFormat.CurrencyGroupSeparator = ",";
    cib.NumberFormat.CurrencyDecimalSeparator = ".";
    cib.NumberFormat.NumberGroupSeparator = ",";
    cib.NumberFormat.NumberDecimalSeparator = ".";

    // add some region info also
    cib.LoadDataFromRegionInfo(new RegionInfo("CA"));
    cib.RegionEnglishName = "RegionInfo derived from French-speaking Canada";

    cib.Register();

    Console.WriteLine("New culture created.");
}
catch (Exception e)
{
    Console.WriteLine(e);
    Console.ReadKey();
}

This code registers the custom culture identified by the culture name in the %WINDIR%\\Globalization system directory, where %WINDIR% is the Windows operating system directory. 此代码在%WINDIR%\\ Globalization系统目录中注册由区域性名称标识的自定义区域性,其中%WINDIR%是Windows操作系统目录。 This will make the custom culture available to other applications. 这将使自定义区域性可用于其他应用程序。

CultureInfo test = new CultureInfo("fr-CACustom");

[Note: in order to have access to the CultureAndRegionInfoBuilder class you need a reference to the sysglobl assembly which is not reference by default in most Visual Studio project templates.] [注意:为了能够访问CultureAndRegionInfoBuilder类,您需要引用sysglobl程序集,大多数Visual Studio项目模板中默认都不引用该程序集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM