c#语言呈现位置信息,在C#中获取当前位置(在区域和语言中指定)

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

如果有人对最终代码感兴趣,我做了以下实用程序类 –

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

///

/// Returns machine current location as specified in Region and Language settings.

///

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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值