Get information about the region of Moldova

I am trying to get region information according to ISO 3166-1. TwoLetters country name is "MD".

var r = new RegionInfo("MD"); 

But I get the following exception:

The culture name "MD" is not supported.

This is strange, because Microsoft Table of the supported countries is present in Moldova:

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

+6
source share
3 answers

According to the MSDN documentation for RegionInfo about crop names:

The predefined crop names are listed in the National Language Support API (NLS) Reference at Go Global Developer Center.

When you go to the National Language Support API (NLS) Reference , MD is not found there.

+4
source

You can create your own culture information .

Launch Visual Studio as an administrator. In your project, add a link to 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); } } } 

You just need to take care of modifying the XML.

Note. It seems that you can also maintain a culture without administrative privileges. Here's the link: A Practical Guide. Preservation of user cultures without administrative privileges . I have not tested it myself, but the only comment on this article seems to indicate that it does not work.

[UPDATE]

This is also an interesting approach (a wrapper for calling your own methods):

Get a complete list of supported Windows C # countries .

+3
source

System.Globalization.RegionInfo uses culture data, and if there is no culture data in this area, then this will fail. That is why the creation of a custom culture for a language with this region will lead to its successful completion.

Instead, you need to use the new Windows.Globalization.GeographicRegion , which supports all current ISO-3166 countries.

Or you can p / Invoke in GetGeoInfo and EnumSystemGeoId, as they support Moldova, as indicated in the documentation mentioned above.

0
source

Source: https://habr.com/ru/post/957414/


All Articles