本文介绍了在C#中,如何获取“国家或地区".在“区域和地区"下选择语言"在Windows 10中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行Windows10.从开始菜单中打开地区和语言设置"时,可以选择国家或地区".我正在尝试在C#程序中获取此值.

I am running Windows 10. When I open "Region & language settings" from the start menu, I can select a "Country or region". I am trying to get this value in a C# program.

我在丹麦.我尝试将国家/地区更改为德国(请参见屏幕截图),但我无法获取我的信息代码返回德国.重新启动计算机没有帮助.

I am in Denmark. I have tried changing my country to Germany (see screenshot), but I cannot get my code to return Germany. Rebooting the computer did not help.

我写了一些受此线程启发的代码.

I have written some code inspired by this thread.

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

My code looks like this (trying various things at once, getting all the region/culture things I can think of):

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

如何获取我的程序以检测到我选择了德国?我需要调用什么方法或属性?并且可能需要重新启动或清除高速缓存吗?

How can I get my program to detect that I have selected Germany? What method or property do I need to call? And what restarts or cache-clearing might be necessary?

推荐答案

我在.

I found the answer to my question in this thread.

我正在使用下面的代码,该代码由@SanjaySingh在该线程中提出,仅进行了少许修改.

I am using the below code, as proposed by @SanjaySingh in that thread and only slightly modified.

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

If I call GetMachineCurrentLocation with the geoFriendlyname parameter set to 5, I get the three-letter ISO region code I want (for Germany this is "DEU").

可以找到geoFriendlyname的值此处.

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
}

这篇关于在C#中,如何获取“国家或地区".在“区域和地区"下选择语言"在Windows 10中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:03