Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Linq;
- using System.Runtime.InteropServices;
- namespace GeoInfoService
- {
- public static class GeoInfoService
- {
- private static readonly Dictionary<string, (string EnglishName, int GeoId, string ISO2Code, string ISO3Code)> _regionCodeMapping
- = CultureInfo.GetCultures(CultureTypes.SpecificCultures)
- .Select(ci => new RegionInfo(ci.LCID))
- .GroupBy(ri => ri.TwoLetterISORegionName)
- .ToDictionary(
- g => g.Key,
- g => g.First().Transform(x => (x.EnglishName, x.GeoId, x.TwoLetterISORegionName, x.ThreeLetterISORegionName))
- );
- public static string GetFriendlyName(string code)
- => _regionCodeMapping[code].EnglishName;
- public enum SYSGEOTYPE
- {
- GEO_NATION = 0x0001,
- GEO_LATITUDE,
- GEO_LONGITUDE,
- GEO_ISO2,
- GEO_ISO3,
- GEO_RFC1766,
- GEO_LCID,
- GEO_FRIENDLYNAME,
- GEO_OFFICIALNAME,
- GEO_TIMEZONES,
- GEO_OFFICIALLANGUAGES,
- GEO_ISO_UN_NUMBER,
- GEO_PARENT,
- GEO_DIALINGCODE,
- GEO_CURRENCYCODE,
- GEO_CURRENCYSYMBOL,
- GEO_NAME,
- GEO_ID
- }
- public static string GetGeoInfoEx(string geoId, SYSGEOTYPE geoType)
- {
- switch (geoType)
- {
- case SYSGEOTYPE.GEO_NATION:
- case SYSGEOTYPE.GEO_RFC1766:
- case SYSGEOTYPE.GEO_LCID:
- case SYSGEOTYPE.GEO_ID:
- throw new Exception("Not supported");
- case SYSGEOTYPE.GEO_TIMEZONES:
- case SYSGEOTYPE.GEO_OFFICIALLANGUAGES:
- throw new Exception("Not implemented in the system");
- case SYSGEOTYPE.GEO_LATITUDE:
- case SYSGEOTYPE.GEO_LONGITUDE:
- case SYSGEOTYPE.GEO_ISO2:
- case SYSGEOTYPE.GEO_ISO3:
- case SYSGEOTYPE.GEO_FRIENDLYNAME:
- case SYSGEOTYPE.GEO_OFFICIALNAME:
- case SYSGEOTYPE.GEO_ISO_UN_NUMBER:
- case SYSGEOTYPE.GEO_PARENT:
- case SYSGEOTYPE.GEO_DIALINGCODE:
- case SYSGEOTYPE.GEO_CURRENCYCODE:
- case SYSGEOTYPE.GEO_CURRENCYSYMBOL:
- case SYSGEOTYPE.GEO_NAME:
- break;
- default:
- throw new Exception("Unexpected SYSGEOTYPE value");
- }
- string s = new string('\0', 256);
- int size = GetGeoInfoEx(geoId, geoType, s, 256);
- if (size <= 0) { return null; }
- return s.Substring(0, size - 1);
- }
- [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
- private extern static int GetGeoInfoEx(string location, SYSGEOTYPE geoType, string geoData, int geoDataCount);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement