.NET CultureInfo in Windows 10
Did you know that the CultureInfo behavior with “unkown” cultures has changed with Windows 10?
I stumbled two times about this “problem” - so this is enough to write a short blogpost about it.
Demo Code
Lets use this democode:
try
{
// ok on Win10, but not on pre Win10 if culture is not registred
CultureInfo culture1 = new CultureInfo("foo");
CultureInfo culture2 = new CultureInfo("xyz");
CultureInfo culture3 = new CultureInfo("en-xy");
// not ok even on Win 10 - exception
CultureInfo culture4 = new CultureInfo("foox");
}
catch (Exception exc)
{
}
Windows 10 Case
If you run this code under Windows 10 it should fail for the “foox” culture, because it doesn’t seem to be a valid culture anyway.
“culture1”, “culture2”, “culture3” are all valid cultures in the Windows 10 world, but are resolved with Unkown Locale and LCID 4096.
I guess Windows will look for a 2 or 3 letter ISO style language, and “foox” doesn’t match this pattern.
Pre Windows 10 - e.g. running on Win Server 2012R2
If you would run the code unter Windows Server 2012 R2 it would fail on the first culture, because there is no “foo” culture registred.
“Problem”
The main “problem” is that this behavior could lead to some production issues if you develop with Windows 10 and the software is running on a Win 2012 server.
If you are managing “language” content in your application, be aware of this “limitation” on older Windows versions.
I discovered this problem while debugging our backend admin application. With this ASP.NET frontend it is possible to add or manage “localized” content and the dropdown for the possible language listed a whole bunch of very special, but “Unkown locale” cultures. So we needed to filter out all LCID 4096 cultures to ensure it would run under all Windows versions.
MSDN
This behavior is also documented on MSDN
The “Unkown culture” LCID 4096 was introduced with Windows Vista, but only with Windows 10 it will be “easy” usable within the .NET Framework.