我该如何获取系统区域与语言设置中指定的机器当前位置?我已经尝试从RegionInfo类中获取此信息,但它返回的是区域和语言中指定的格式下拉菜单中的位置。为了澄清我的意思,如果您打开计算机的控制面板中的区域和语言设置,我想读取在位置选项卡中指定的位置。 RegionInfo给出的值是指定格式选项卡中的格式下拉菜单。
经过大量的搜索,我终于得到了答案。以下两个链接帮助我获取当前机器位置:
http://social.msdn.microsoft.com/Forums/eu/csharpgeneral/thread/6dfaa142-c588-4cb0-b044-fa1e8138b299
http://www.siao2.com/2007/02/21/1733999.aspx
如果有兴趣查看最终代码,我编写了以下实用类:
public static class RegionAndLanguageHelper
{
#region Constants
private const int GEO_FRIENDLYNAME = 8;
#endregion
#region Private Enums
private enum GeoClass : int
{
Nation = 16,
Region = 14,
};
#endregion
#region Win32 Declarations
[DllImport("kernel32.dll", ExactSpelling = true, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
private static extern int GetUserGeoID(GeoClass geoClass);
[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
#region Public Methods
/// <summary>
/// Returns machine current location as specified in Region and Language settings.
/// </summary>
public static string GetMachineCurrentLocation()
{
int geoId = GetUserGeoID(GeoClass.Nation); ;
int lcid = GetUserDefaultLCID();
StringBuilder locationBuffer = new StringBuilder(100);
GetGeoInfo(geoId, GEO_FRIENDLYNAME, locationBuffer, locationBuffer.Capacity, lcid);
return locationBuffer.ToString().Trim();
}
#endregion
}
using (var regKeyGeo = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Control Panel\International\Geo")) { Response.Write("<br />Geo Nation " + regKeyGeo.GetValue("Nation").ToString()); }
- Kiquenet根据“控制面板>区域>家庭位置”,您可以获得RegionInfo。尝试以下操作 -
var regKeyGeoId = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Control Panel\International\Geo");
var geoID = (string)regKeyGeoId.GetValue("Nation");
var allRegions = CultureInfo.GetCultures(CultureTypes.SpecificCultures).Select(x => new RegionInfo(x.ToString()));
var regionInfo = allRegions.FirstOrDefault(r => r.GeoId == Int32.Parse(geoID));
你能试着使用
RegionInfo.CurrentRegion.DisplayName;
这个能够按照你的意图提供所需的位置名称吗?
是的.. 但更简单:
CultureInfo info = CultureInfo.CurrentCulture;