Last active
March 28, 2021 13:13
-
-
Save KillyMXI/66f0545c061cb58994a037d1109cef74 to your computer and use it in GitHub Desktop.
GetGeoInfoEx C# example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
At some point I was looking into using
CultureInfo
orGetGeoInfoEx
to get some information based on country code.I opted for a different solution since then, but I decided to dump this here in case I will ever need something like this.
This code is not quite complete. I discarded it before I was completely sure which types of information are actually supported.
As for the solution I decided to use: there are multiple projects on GitHub, trying to provide country information, all without much support and with some inconveniences and errors. I patched some of them for my needs. I might even publish my (N+1)th project too at some point.
If I find a way to generate an accurate code from some open geo-informational system - that will be the way to go.
That's the appeal of this Gist - it relies on sources maintained by someone else and expected to have certain level of quality. Although they happen to have no information I was after.