|
| 1 | +using System.Runtime.InteropServices; |
| 2 | +using System.Text; |
| 3 | + |
| 4 | +namespace OpenHardwareMonitor; |
| 5 | + |
| 6 | +public enum SysGeoType { |
| 7 | + GEO_NATION = 0x0001, |
| 8 | + GEO_LATITUDE = 0x0002, |
| 9 | + GEO_LONGITUDE = 0x0003, |
| 10 | + GEO_ISO2 = 0x0004, |
| 11 | + GEO_ISO3 = 0x0005, |
| 12 | + GEO_RFC1766 = 0x0006, |
| 13 | + GEO_LCID = 0x0007, |
| 14 | + GEO_FRIENDLYNAME = 0x0008, |
| 15 | + GEO_OFFICIALNAME = 0x0009, |
| 16 | + GEO_TIMEZONES = 0x000A, |
| 17 | + GEO_OFFICIALLANGUAGES = 0x000B, |
| 18 | + GEO_ISO_UN_NUMBER = 0x000C, |
| 19 | + GEO_PARENT = 0x000D, |
| 20 | + GEO_DIALINGCODE = 0x000E, |
| 21 | + GEO_CURRENCYCODE = 0x000F, |
| 22 | + GEO_CURRENCYSYMBOL = 0x0010, |
| 23 | + GEO_NAME = 0x0011, |
| 24 | + GEO_ID = 0x0012 |
| 25 | +} |
| 26 | + |
| 27 | +public static class RegionHelper |
| 28 | +{ |
| 29 | + private enum GeoClass |
| 30 | + { |
| 31 | + Nation = 16, |
| 32 | + Region = 14, |
| 33 | + }; |
| 34 | + |
| 35 | + [DllImport("kernel32.dll", ExactSpelling = true, CallingConvention = CallingConvention.StdCall, SetLastError = true)] |
| 36 | + private static extern int GetUserGeoID(GeoClass geoClass); |
| 37 | + |
| 38 | + [DllImport("kernel32.dll")] |
| 39 | + private static extern int GetUserDefaultLCID(); |
| 40 | + |
| 41 | + [DllImport("kernel32.dll")] |
| 42 | + private static extern int GetGeoInfo(int geoid, int geoType, StringBuilder lpGeoData, int cchData, int langid); |
| 43 | + |
| 44 | + public static string GetGeoInfo(SysGeoType geoType = SysGeoType.GEO_FRIENDLYNAME) |
| 45 | + { |
| 46 | + int geoId = GetUserGeoID(GeoClass.Nation); |
| 47 | + int lcid = GetUserDefaultLCID(); |
| 48 | + var buffer = new StringBuilder(100); |
| 49 | + GetGeoInfo(geoId, (int)geoType, buffer, buffer.Capacity, lcid); |
| 50 | + return buffer.ToString().Trim(); |
| 51 | + } |
| 52 | +} |
0 commit comments