在C#中,如何在Windows 10中获取"Region & language"下选中的"Country or region"?

In C#, how to get the "Country or region" selected under "Region & language" in Windows 10?

我是运行 Windows 10.当我从开始菜单打开"Region & language settings"时,我可以select一个"Country or region"。我试图在 C# 程序中获取此值。

我在丹麦。我已尝试将我的国家更改为德国(请参阅 screenshot),但我无法将我的代码更改为 return 德国。重新启动计算机没有帮助。

我写了一些受 this thread 启发的代码。

我的代码看起来像这样(一次尝试各种事情,获得​​我能想到的所有 region/culture 事情):

private static void Main(string[] args)
{
    Thread.CurrentThread.CurrentCulture.ClearCachedData();
    Thread.CurrentThread.CurrentUICulture.ClearCachedData();
    var thread = new Thread(() => ((Action) (() =>
    {
        Console.WriteLine("Current culture: {0}", Thread.CurrentThread.CurrentCulture.Name);
        Console.WriteLine("Current UI culture: {0}", Thread.CurrentThread.CurrentUICulture.Name);
        Console.WriteLine("Installed UI culture: {0}", CultureInfo.InstalledUICulture.Name);
        Console.WriteLine("Current region: {0}", RegionInfo.CurrentRegion.ThreeLetterISORegionName);
        Console.WriteLine("System default LCID: {0}", GetSystemDefaultLCID());
    }))());
    thread.Start();
    thread.Join();
    Console.ReadKey();
}

[DllImport("kernel32.dll")]
private static extern uint GetSystemDefaultLCID();

它输出:

Current culture: en-DK
Current UI culture: en-US
Installed UI culture: en-US
Current region: DNK
System default LCID: 1033

如何让我的程序检测到我已经 selected 德国?我需要调用什么方法或属性?什么重启或缓存清除可能是必要的?

Read msdn documentation: RegionInfo Properties

var regionInfo = RegionInfo.CurrentRegion;
var name = regionInfo.Name;
var englishName = regionInfo.EnglishName;
var displayName = regionInfo.DisplayName;

Console.WriteLine("Name: {0}", name);
Console.WriteLine("EnglishName: {0}", englishName);
Console.WriteLine("DisplayName: {0}", displayName);   

Name: DE
EnglishName: Germany
DisplayName: Germany

我在 this thread 中找到了问题的答案。

我正在使用下面的代码,正如@SanjaySingh 在该线程中所建议的那样,并且只是稍作修改。

如果我调用 GetMachineCurrentLocation 并将 geoFriendlyname 参数设置为 5,我会得到我想要的三个字母的 ISO 区域代码(对于德国,这是 "DEU" ).

可以找到 geoFriendlyname 的值 here

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>
    /// <param name="geoFriendlyname"></param>
    public static string GetMachineCurrentLocation(int geoFriendlyname)
    {
        int geoId = GetUserGeoID(GeoClass.Nation); ;
        int lcid = GetUserDefaultLCID();
        StringBuilder locationBuffer = new StringBuilder(100);
        GetGeoInfo(geoId, geoFriendlyname, locationBuffer, locationBuffer.Capacity, lcid);

        return locationBuffer.ToString().Trim();
    }

    #endregion
}

尝试

    var geographicRegion = new Windows.Globalization.GeographicRegion();
    var code = geographicRegion.CodeTwoLetter;

    var region = Windows.System.UserProfile.GlobalizationPreferences.HomeGeographicRegion;

比选择的答案短得多,并且不需要 DllImports 或 ClearCachedData:

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));
Console.WriteLine("EnglishName:" + regionInfo.EnglishName);
Console.WriteLine("DisplayName:" + regionInfo.DisplayName);
Console.WriteLine("NativeName:" + regionInfo.NativeName);
Console.WriteLine("ISOCurrencySymbol:" + regionInfo.ISOCurrencySymbol);
Console.WriteLine("Name:" + regionInfo.Name);

这会读取您在 "Regional & Language" > "Country or region" 下的 Windows 10 中设置的信息。

感谢@Zan,因为我的只是他 post 的延伸:Get Current Location (as specified in Region and Language) in C#