我正在运行Windows 10.当我打开" Region&语言设置"从开始菜单中,我可以选择一个"国家或地区"。我想在C#程序中获得这个值。
我在丹麦。我曾尝试将我的国家改为德国(见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
如何让我的程序检测到我选择了德国?我需要调用什么方法或属性?什么重启或缓存清理可能是必要的?
答案 0 :(得分:2)
我在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
}
答案 1 :(得分:1)
答案 2 :(得分:0)
尝试
var geographicRegion = new Windows.Globalization.GeographicRegion();
var code = geographicRegion.CodeTwoLetter;
或
var region = Windows.System.UserProfile.GlobalizationPreferences.HomeGeographicRegion;
答案 3 :(得分:0)
比选择的答案短得多,并且不需要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,因为我只是他的帖子的延伸:Get Current Location (as specified in Region and Language) in C#