在C#中,如何获取Windows 10中“区域和语言”下选择的“国家或地区”?
我正在运行Windows 10.当我从开始菜单打开“区域&语言设置”时,我可以选择“国家或地区”。我正在尝试在C#程序中获取此值。在C#中,如何获取Windows 10中“区域和语言”下选择的“国家或地区”?
我在丹麦。我曾尝试将我的国家改为德国(请参阅screenshot),但我无法让我的代码返回德国。重新启动电脑没有帮助。
我写了一些代码灵感来自this thread。
我的代码看起来像这样(试图马上各种各样的事情,让所有我能想到的区域/文化的东西):
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
我怎样才能得到我的程序检测到我选择了德国?我需要拨打什么方法或财产?什么重新启动或缓存清除可能是必要的?
我找到了答案,我的问题在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
}
你能告诉我你传递了什么'GetMachineCurrentLocation(?)',并且你通过调用'GetMachineCurrentLocation()'方法得到了什么? –
在我的情况:'var test = RegionAndLanguageHelper.GetMachineCurrentLocation(RegionInfo.CurrentRegion.GeoId);'=>'test'是“”(空) –
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);
名称:DE
中文名:德国
显示名称:德国
我试过了,正如我原来的帖子所示。它没有帮助。当我在“地区和语言设置”中更改我的国家时,CurrentThread.CurrentCulture没有改变。 –
@ClausAppel你尝试过'var regionInfo = RegionInfo.CurrentRegion;'?为我工作。 –
@ClausAppel,来自msdn:*“RegionInfo类不会自动检测系统设置中的更改,而是[CurrentRegion](https://msdn.microsoft.com/en-us/library/system.globalization.regioninfo。 currentregion(v = vs.110).aspx)属性在您调用[ClearCachedData](https://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.clearcacheddata(v = vs。 110).aspx)方法“*。 – Sinatr
尝试
var geographicRegion = new Windows.Globalization.GeographicRegion();
var code = geographicRegion.CodeTwoLetter;
或
var region = Windows.System.UserProfile.GlobalizationPreferences.HomeGeographicRegion;
这是UWP解决方案。 – Sinatr
[RegionInfo.CurrentRegion](https://msdn.microsoft.com/en-us/library/system.globalization.regioninfo。 currentregion(v = vs.110)的.aspx)。有时间升级您的Google-Fu。 –
区域控制面板[Win32]中存在“管理”选项卡,而不是现代设置。点击“复制设置”和“更改系统区域设置”。可能可能会解决你的quastion –
我终于在这个线程中找到了我的问题的答案:https://stackoverflow.com/questions/8879259/get-current-location-as-specified-in-region-and-language-in- c-sharp –