.netinternationalizationregioninfo

RegionInfo and GeoId


I was thinking of using RegionInfo.TwoLetterISORegionName to as a database attribute from which to get information about a country. Until I saw RegionInfoGeoId, which seems more robust.

Which would you store to support internationalization? How would you create a RegionInfo from a GeoId?

Cheers,
Berryl


Solution

  • Using P/Invoke and the GetGeoInfo function, you can get some information from a GeoID:

        public static string GetGeoInfo(int geoId, SYSGEOTYPE geoType)
        {
            string s = new string('\0', 256);
            int size = GetGeoInfo(geoId, geoType, s, 256, 0);
            if (size <= 0)
                return null;
    
            return s.Substring(0, size - 1);
        }
    
        public enum SYSGEOTYPE
        {
            GEO_NATION = 0x0001,
            GEO_LATITUDE = 0x0002,
            GEO_LONGITUDE = 0x0003,
            GEO_ISO2 = 0x0004,
            GEO_ISO3 = 0x0005,
            GEO_RFC1766 = 0x0006,
            GEO_LCID = 0x0007,
            GEO_FRIENDLYNAME = 0x0008,
            GEO_OFFICIALNAME = 0x0009,
            GEO_TIMEZONES = 0x000A,
            GEO_OFFICIALLANGUAGES = 0x000B
        }
    
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private extern static int GetGeoInfo(int geoId, SYSGEOTYPE geoType, string lpGeoData, int cchData, int language);
    

    But, beware, you may not have a 1:1 correspondence between a Geographic location and a Region.