以编程方式获取在区域设置中设置的位置

时间:2015-01-14 02:10:36

标签: c# .net vb.net

我在获取PC的位置设置时遇到了一些麻烦。我意识到很容易获得文化设置,但我正在谈论根据以下对话框的家庭位置设置。我使用,但也很好。

this is what i mean

2 个答案:

答案 0 :(得分:0)

您可以像这样使用RegionInfo类:

String currentRegion= System.Globalization.RegionInfo.CurrentRegion.DisplayName;

答案 1 :(得分:0)

所以,我从powershell cmdlet Get-WinHomeLocation中删除了它:

如果您声明以下类:

public static class GeoTools
{
    [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern int GetUserGeoID(int GeoClass);
    [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern int GetGeoInfo(int Location, 
                                        uint GeoType, 
                                        StringBuilder GeoData, 
                                        int Length, 
                                        ushort LangID);
}

然后你可以:

uint GEO_FRIENDLYNAME = 8u;
var homeLocationGeoId=GeoTools.GetUserGeoID(16);
StringBuilder stringBuilder = new StringBuilder(256);
int geoInfo = GeoTools.GetGeoInfo(homeLocationGeoId, 
                                  GEO_FRIENDLYNAME, 
                                  stringBuilder, 
                                  256, 
                                  0);
var homeLocation = stringBuilder.ToString(); //yay!