您可以在以下方面更改测量系统:
=>时间和语言=>附加日期、时间和区域设置=>更改日期、时间或数字格式=>附加设置=>测量系统
在RegionInfo.CurrentRegion.IsMetric
中使用C#时,这些数据与您在设置中选择的内容无关。
如何从C#代码中访问当前选择的测量系统?
发布于 2019-02-08 11:05:43
好的,我找到了解决方案(C#):Microsoft.Win32.Registry.GetValue(@"HKEY_CURRENT_USER\Control Panel\International", "iMeasure", 0)
是Windows注册表存储有关所选测量系统的信息(而不是绑定到区域的信息)。
发布于 2022-05-14 16:51:20
正如MS所记录的那样,RegionInfo.CurrentRegion.IsMetric
应该能工作:
此属性的值基于通过“控制面板”的“区域和语言选项”部分选择的区域性。然而,在AppDomain的生命周期中,该信息可能会发生变化。RegionInfo类不会自动检测系统设置中的更改,但在调用ClearCachedData方法时将更新ClearCachedData属性。 RegionInfo.CurrentRegion性质
我在.NET Core6.0上进行了测试,每次更改控制面板中的单元时,Console.WriteLine(RegionInfo.CurrentRegion.IsMetric);
都会打印出正确的值。您只需要在更改区域选项之后重新启动应用程序,或者调用ClearCachedData()
。
发布于 2020-10-21 18:09:22
请记住,在度量/美国、之间手动转换单位时,单行代码大多数情况下都不能工作,因此不要使用下面的行。
System.Globalization.RegionInfo.CurrentRegion.IsMetric;
@salverio所写的作品效果很好。
using System;
using Microsoft.Win32;
public static bool IsMetric(string KeyPath,
string KeyName)
{
try
{
string input;
input = ReadRegistry(KeyPath, KeyName);
if (input == "0")
{
return true;
}
else if (input == "1")
{
return false;
}
return System.Globalization.RegionInfo.CurrentRegion.IsMetric;
}
catch (Exception ex)
{
return System.Globalization.RegionInfo.CurrentRegion.IsMetric;
}
}
public static string ReadRegistry(string path,
string name)
{
try
{
string output = "";
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(path))
{
if (key != null)
{
object o = key.GetValue(name);
if (o != null)
{
output = o as string;
}
}
}
return output;
}
catch (Exception ex)
{
return "";
}
}
只需调用函数IsMetric(注册表项路径、注册表项名称)即可。
哪里,
KeyPath = @"Control Panel\International";
KeyName = "iMeasure";
https://stackoverflow.com/questions/54575900
复制相似问题