首页 文章

获取摩尔多瓦的地区信息

提问于
浏览
6

我正在尝试通过ISO 3166-1 TwoLetters国家名称 - “MD”获取区域信息 .

var r = new RegionInfo("MD");

但我得到以下例外:

不支持文化名称'MD' .

这很奇怪,因为Microsoft支持的国家表摩尔多瓦存在:

http://msdn.microsoft.com/en-us/library/dd374073.aspx

3 回答

  • 0

    根据MSDN documentation on RegionInfo关于文化名称:

    预定义文化名称列在Go Global开发人员中心的国家语言支持(NLS)API参考中 .

    当你去National Language Support (NLS) API Reference时,在那里找不到 MD .

  • 4

    你可以create your own culture info .

    以管理员身份运行Visual Studio . 在项目中,添加对 sysglobl 的引用 .

    using System;
    using System.IO;
    using System.Globalization;
    using System.Linq;
    using System.Xml.Linq;
    
    class Program
    {
        public static void Main()
        {
            CultureAndRegionInfoBuilder cib = null;
            try
            {
                // Create a CultureAndRegionInfoBuilder 
                // object named "ro-MD".
                cib = new CultureAndRegionInfoBuilder(
                                        "ro-MD", CultureAndRegionModifiers.None);
    
                // Populate the new CultureAndRegionInfoBuilder 
                // object with culture information.
                CultureInfo ci = new CultureInfo("ro-RO");
                cib.LoadDataFromCultureInfo(ci);
    
                // Populate the new CultureAndRegionInfoBuilder 
                // object with region information.
                RegionInfo ri = new RegionInfo("RO");
                cib.LoadDataFromRegionInfo(ri);
    
                var filePath = "ro-MD.xml";
    
                if (File.Exists(filePath))
                    File.Delete(filePath);
    
                // Save as XML
                cib.Save(filePath);
    
                // TODO: modify the XML
                var xDoc = XDocument.Load(filePath);
                var ns = 
                    "http://schemas.microsoft.com/globalization/2004/08/carib/ldml";
    
                xDoc.Descendants(XName.Get("regionEnglishName", ns))
                    .FirstOrDefault().Attribute("type").SetValue("Moldova");
    
                xDoc.Descendants(XName.Get("regionNativeName", ns))
                    .FirstOrDefault().Attribute("type").SetValue("Moldova");
    
                // and so on
                xDoc.Save(filePath);
    
                var roMd = CultureAndRegionInfoBuilder
                    .CreateFromLdml(filePath);
    
                // this may throw an exception if the culture info exists 
                try
                {
                    CultureAndRegionInfoBuilder.Unregister("ro-MD");
                }
                catch (Exception)
                {
                    //throw;
                }
    
                // Register the custom culture.
                roMd.Register();
    
                // Display some of the properties of the custom culture.
                var riMd = new RegionInfo("ro-MD");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
    }
    

    您只需要处理XML修改 .

    注意:您似乎也可以在没有管理权限的情况下保存文化 . 这是一个参考:How to: Save Custom Cultures Without Administrative Privileges . 我没有't tested it myself, but the only comment on that article seems to suggest it doesn'工作 .

    [UPDATE]

    这也是一种有趣的方法(用于调用本机方法的包装器):

    Get the full list of Windows supported countries with C# .

  • 3

    System.Globalization.RegionInfo使用文化数据,如果该区域没有文化数据,那么它将不会成功 . 这也是为什么为具有该区域的语言创建自定义文化将使其成功的原因 .

    你可能想要的是使用新的Windows.Globalization.GeographicRegion,它支持所有当前的ISO-3166国家 .

    或者您可以按照上面引用的文档中的说明p / Invoke到GetGeoInfo和EnumSystemGeoId,因为它们支持Moldova .

相关问题