开发者

How to programatically retrieve the "Location" as displayed in the Regional and Language Settings?

开发者 https://www.devze.com 2022-12-22 02:03 出处:网络
How can I programatically retrieve the \"Location\" as displayed in the Regional and Language Settings?

How can I programatically retrieve the "Location" as displayed in the Regional and Language Settings?

alt text 开发者_如何学JAVAhttp://dl.dropbox.com/u/3978473/location.png


Here is a list of the GeoIDs as you would retrieve from the registry entry previously mentioned: (they're in hex)

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

Entry:

HKEY_CURRENT_USER\Control Panel\International\Geo


There is an API function which returns the GEOID of the selected location:

GEOID locationId = GetUserGeoID(GEOCLASS_NATION);

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


HKEY_CURRENT_USER\Control Panel\International\Geo

I don't know, how to convert the number to the name of the country :(


Maybe useful code (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);
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消