Freigeben über


On Windows 7, the Invariant Culture is an installed culture...

Last month, I investigated an issue for an ISV where their code would work fine on Windows Vista but fail on Windows 7. Not very commom!

The cause?

CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures)

returns an array that does not contain the CultureInfo.InvariantCulture on Windows Vista but does on Windows 7.

On Windows 7, the Invariant Culture is considered to be an installed culture. It was not on Windows Vista. From Eric:

“The Invariant culture is normally included with NeutralCultures enumerations. It is also specifically excluded from SpecificCulture enumerations. However, there is no attempt to remove it from the InstalledWin32Cultures enumeration and since CultureTypes.AllCultures == CultureTypes.NeturalCultures | CultureTypes.SpecificCultures | CultureTypes.InstalledWin32Cultures, the InvariantCulture is showing up as one of the InstalledWin32Cultures.”

Depending on what you do with the result, this might be an issue. For example if you create an instance of RegionInfo RegionInfo(CultureInfo.InvariantCulture.LCID), this will result in an ArgumentException exception being raised.

To make sure your code works on both O.S., you might need to introduce the highlighted code below:

var list = new List<RegionInfo>();

int i = 0;

foreach (var cultureInfo in CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures)) {

   _cultureInfoComboBox.Items.Add(String.Format("{0} : {1}", i++, cultureInfo.NativeName));

   if (!cultureInfo.Equals(CultureInfo.InvariantCulture)) {

      var regionCulture = new RegionInfo(cultureInfo.LCID);

      if (!list.Contains(regionCulture)) {

         list.Add(regionCulture);

         _countriesSelector.Items.Add(regionCulture.DisplayName);

      }

   }

}

if (_countriesSelector.Items.Count > 0) {

   _countriesSelector.SelectedIndex = 0;

}

if (_cultureInfoComboBox.Items.Count > 0) {

   _cultureInfoComboBox.SelectedIndex = 0;

}

 

J’espère que vous trouverez cela utile. Buen día.