【问题标题】:Get Current Location (as specified in Region and Language) in C#在 C# 中获取当前位置(在区域和语言中指定)
【发布时间】:2012-01-16 11:20:02
【问题描述】:

如何获取操作系统区域和语言中指定的机器当前位置?我已经尝试从RegionInfo 类中获取此信息,但它会返回区域和语言的格式下拉菜单中指定的位置。

只是为了澄清我的意思,如果您从机器的控制面板打开区域和语言,我想阅读位置选项卡中指定的位置。 RegionInfo 为我提供了“格式”选项卡的“格式”下拉菜单中指定的值。

【问题讨论】:

  • @Sai 我认为 OP 是指地理位置。
  • 我认为他希望像 乌克兰、美国、英国这样的位置一样明智,与文化无关
  • 没错。例如,我可以将我的文化设置为英语(美国),并将我的位置设置为印度。您可以在区域和语言控制面板中单独更改这两者。

标签: c# localization


【解决方案1】:

经过大量的谷歌搜索,我终于得到了答案。以下两个链接帮助我获取当前机器位置-

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
}

【讨论】:

  • 这是在多个操作系统(如 Win7 + Win8)上唯一有效的解决方案。我不明白为什么根本没有评分...
  • using (var regKeyGeo = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Control Panel\International\Geo")) { Response.Write("&lt;br /&gt;Geo Nation " + regKeyGeo.GetValue("Nation").ToString()); }
  • @Kiquenet 我绝对会避免使用注册表。这通常是无证和不受支持的,你可以很确定它会在某个时候中断。
  • 太长了,不像@Zan 下面所说的那么优雅。
【解决方案2】:

你可以尝试使用

RegionInfo.CurrentRegion.DisplayName;

这是否为您提供所需的位置名称

【讨论】:

  • 我已经尝试过了,但它给了我当前的格式,而不是区域和语言对话框(控制面板->区域和语言)中指定的当前位置
  • 不会工作。我的位置是日本,但上面显示的是“美国”。
  • 不相同 Control Panel: Regional and language options-&gt;Standards and formatsControl Panel: Regional and language options-&gt;Location
【解决方案3】:

基于“控制面板>区域>家庭位置”,您可以获得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));

【讨论】:

【解决方案4】:

是的..但更容易:

CultureInfo info = CultureInfo.CurrentCulture;

【讨论】:

  • 那行不通。例如,我的位置选项卡设置为日本。但是,上面的代码生成了 en-US。
  • 不相同 Control Panel: Regional and language options-&gt;Standards and formatsControl Panel: Regional and language options-&gt;Location
猜你喜欢
  • 1970-01-01
  • 2010-12-05
  • 2011-05-11
  • 1970-01-01
  • 1970-01-01
  • 2014-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多