Skip to content

Instantly share code, notes, and snippets.

@semuserable
Last active April 22, 2016 15:49
Show Gist options
  • Save semuserable/19fe3bb4931f7f52e3a05da9d0fcdea2 to your computer and use it in GitHub Desktop.
Save semuserable/19fe3bb4931f7f52e3a05da9d0fcdea2 to your computer and use it in GitHub Desktop.
[System.Runtime.InteropServices.DllImport("KERNEL32.DLL")]
private static extern int GetSystemDefaultLCID();
private RegionInfo CurrentRegionInfo()
{
var cultureInfo = new CultureInfo(GetSystemDefaultLCID());
return new RegionInfo(cultureInfo);
}
// OR (it's preferable if Vista or higher)
[System.Runtime.InteropServices.DllImport("KERNEL32.DLL")]
private static extern int GetSystemDefaultLocaleName(byte[] buffer, int bufferSize);
private RegionInfo CurrentRegionInfo()
{
var byteLength = 512;
var bytes = new byte[byteLength];
GetSystemDefaultLocaleName(bytes, byteLength);
var currentCultureName = Encoding.Unicode.GetString(bytes);
var cultureInfo = new CultureInfo(currentCultureName);
return new RegionInfo(cultureInfo.LCID);
}
// OR (most correct way)
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
private static extern int GetLocaleInfoEx(string localeName, Lctype lcType, byte[] buffer, int bufferSize);
[DllImport("kernel32.dll")]
private static extern int GetSystemDefaultLocaleName(byte[] buffer, int bufferSize);
public enum Lctype : uint
{
LocaleImeasure = 0x0000000D, // 0 = metric, 1 = inches (US)
}
private int SystemMeasurmentCode()
{
var buffer = new byte[128];
GetLocaleInfoEx(GetCiLocale(), Lctype.LocaleImeasure, buffer, 128);
return int.Parse(Encoding.Unicode.GetString(buffer));
}
private string GetCiLocale()
{
var buffer = new byte[128];
GetSystemDefaultLocaleName(buffer, 128);
return Encoding.Unicode.GetString(buffer);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment