如何以编程方式检索区域和语言设置中显示的“位置”?

时间:2010-03-04 11:58:27

标签: windows winapi windows-7 windows-vista windows-xp

如何以编程方式检索区域和语言设置中显示的“位置”?

alt text http://dl.dropbox.com/u/3978473/location.png

4 个答案:

答案 0 :(得分:2)

以下是您从前面提到的注册表项中检索的GeoID列表:(它们是十六进制)

http://msdn.microsoft.com/en-us/library/dd374073%28VS.85%29.aspx

项:

HKEY_CURRENT_USER\Control Panel\International\Geo

答案 1 :(得分:2)

有一个API函数可以返回所选位置的GEOID:

GEOID locationId = GetUserGeoID(GEOCLASS_NATION);

请参阅http://msdn.microsoft.com/en-us/library/dd318138(v=VS.85).aspx

答案 2 :(得分:1)

  
    

HKEY_CURRENT_USER \ Control Panel \ International \ Geo

  

我不知道,如何将号码转换为国家名称:(

答案 3 :(得分:1)

也许 有用的代码 (C#)

private const int GEOCLASS_NATION = 16;

//SYSGEOTYPE
private const int GEO_NATION = 1;
private const int GEO_LATITUDE = 2;
private const int GEO_LONGITUDE = 3;
private const int GEO_ISO2 = 4;
private const int GEO_ISO3 = 5;
private const int GEO_RFC1766 = 6;
private const int GEO_LCID = 7;
private const int GEO_FRIENDLYNAME = 8;
private const int GEO_OFFICIALNAME = 9;
private const int GEO_TIMEZONES = 10;
private const int GEO_OFFICIALLANGUAGES = 11;


#region Win32 Declarations

// [DllImport("kernel32.dll", ExactSpelling = true, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
// private static extern int GetUserGeoID(GeoClass geoClass);

[DllImport("kernel32.dll")]
static extern int GetUserGeoID(int geoId);

[DllImport("kernel32.dll")]
private static extern int GetUserDefaultLCID();

[DllImport("kernel32.dll")]
private static extern int GetGeoInfo(int geoid, int geoType, StringBuilder lpGeoData, int cchData, int langid);

#endregion



private string GetGeoFriendlyName(int geoId)
{
    int lcid = GetUserDefaultLCID();
    StringBuilder bldr = new StringBuilder(50);
    GetGeoInfo(geoId, GEO_FRIENDLYNAME, bldr, bldr.Capacity, lcid);
    return bldr.ToString();
}

private string GetGeoIso2(int geoId)
{
    int lcid = GetUserDefaultLCID();
    StringBuilder bldr = new StringBuilder(50);
    GetGeoInfo(geoId, GEO_ISO2, bldr, bldr.Capacity, lcid);
    return bldr.ToString();
}




     // HKEY_CURRENT_USER\Control Panel\International\Geo"
    using (var regKeyGeo = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Control Panel\International\Geo"))
    {
         var NationValue = Convert.ToInt32(regKeyGeo.GetValue("Nation").ToString());

         int geoId = GetUserGeoID(GEOCLASS_NATION);
         string friendlyName = GetGeoFriendlyName(geoId);
         Response.Write("GetUserGeoID: " + geoId + " - friendlyName: " + friendlyName + @". HKEY_CURRENT_USER\Control Panel\International\Geo - Nation: " + NationValue + " - " + GetGeoFriendlyName(NationValue));

         var iso2 = GetGeoIso2(geoId);
         Response.Write(" - GetGeoIso2: " + iso2);

         var regionGeo = new RegionInfo(iso2);
        Response.Write(" - " + regionGeo.DisplayName + " - " + regionGeo.ISOCurrencySymbol);

        var cultureInfo = CultureInfo.GetCultures(CultureTypes.AllCultures).Where(c => c.Name.EndsWith(iso2));

        var culture = cultureInfo.ToList()[0];
        Response.Write("<br /> Culture for " + iso2 + ": " + culture.DisplayName + " - " + culture.DateTimeFormat.FullDateTimePattern);
    }