简体   繁体   中英

Access data of additional settings of region on Windows 10

You can change measurement system in:

Windows settings => Time & Language => Additional date, time & regional settings => change date, time, or number formats => Additional settings => Measurement system

When you use RegionInfo.CurrentRegion.IsMetric in C#, this data isn't related to what you have chosen in settings.

How to access from C# code currently chosen Measurement system?

OK, I found that solution (C#): Microsoft.Win32.Registry.GetValue(@"HKEY_CURRENT_USER\Control Panel\International", "iMeasure", 0)

It's Windows registry that stores info about chosen measurement system (not the one bound to region).

RegionInfo.CurrentRegion.IsMetric should work, as documented by MS:

The value of this property is based on the culture selected through the regional and language options portion of Control Panel. However, that information can change during the life of the AppDomain . The RegionInfo class does not automatically detect changes in the system settings, but the CurrentRegion property is updated when you call the ClearCachedData method.

RegionInfo.CurrentRegion Property

I've tested on .NET Core 6.0, Console.WriteLine(RegionInfo.CurrentRegion.IsMetric); prints out the correct value every time I change the unit in Control Panel. You just need to relaunch the app after changing the regional options, or call ClearCachedData()

Keep in mind, this below single line code doesn't work most of the time while manually changing units between Metric/US, SO DO NOT USE THIS BELOW LINE

System.Globalization.RegionInfo.CurrentRegion.IsMetric;

What @salverio wrote works perfectly.

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 "";
    }
}

Just simply call the function IsMetric(registry key path, registry key name)

Where,

KeyPath = @"Control Panel\International";
KeyName = "iMeasure";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM