我运行的是 Windows 10。当我从开始菜单中打开“区域和语言设置”时,我可以选择“国家或地区”。我正在尝试在 C# 程序中获取该值。
我在丹麦。我尝试将我的国家/地区更改为德国(请参阅),但我无法获取返回德国的代码。重新启动计算机没有帮助。
我编写了一些受此线程启发的代码。
我的代码看起来像这样(一次尝试各种事情,得到我能想到的所有地区/文化的东西):
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
如何让我的程序检测到我选择了德国?我需要调用什么方法或属性?可能需要什么重新启动或清除缓存?
我在这个帖子中找到了我的问题的答案。
我正在使用下面的代码,由 @SanjaySingh 在该线程中提出,仅稍加修改。
如果我调用
GetMachineCurrentLocation
并将 geoFriendlyname
参数设置为 5
,我会得到我想要的三个字母的 ISO 区域代码(对于德国,这是 "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
}
阅读 msdn 文档: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
英文名称: 德国
显示名称:德国
比所选答案短得多,并且不需要 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);
这将读取您在 Windows 10 中“区域和语言”>“国家或地区”下设置的信息。
感谢@Zan,因为我的只是他帖子的延伸:在 C# 中获取当前位置(如区域和语言中指定)
如果您使用 GoogleMaps,则它是 ISO-3166 RegionInfo.CurrentRegion.TwoLetterISORegionName
尝试
var geographicRegion = new Windows.Globalization.GeographicRegion();
var code = geographicRegion.CodeTwoLetter;
或
var region = Windows.System.UserProfile.GlobalizationPreferences.HomeGeographicRegion;