CultureInfo Class in C#

Windows 8 Exception

When you create an application in C# and you get the error above, you can know… Culture info. Why is this all of a sudden a problem? With Windows 8, Microsoft changed the formatting for English (South Africa), most notably, the numbering format – from 12.95 in Windows 7 to 12,95 in Windows 8 (and the date 2014-12-04 vs 2014/12/04). Note the full stop versus the comma (and the slashes and dashes).

When an application stores and reads numbers and / or dates, it uses the current locale / regional settings. The problem that you might have here, is that when the application was written, it may have been written to expect a specific formatted data type. This is where the culture info class comes into play. You define a new instance of the Culture Info class as follows in C#:

System.Globalization.CultureInfo CINF = new System.Globalization.CultureInfo("");

In the line above, a new instance of the invariant culture is defined (CINF). The invariant culture is associated with the English language but not with any specific country or region. For this reason, the invariant culture is considered good and useful to use, especially when you create software that will be used with the English language. If the software will be used where operating systems or user customization might change, you will need to use the specific culture.

Once defined, the invariant culture (CINF), needs to be used for parsing and persisting numbers (or any formatted, regionalised data types). It is also considered good programming practice to parse a stored value or to store a value using the culture info class – albeit invariant, neutral, specific or custom cultures. The invariant culture is then used as follows:

Storing a file with numbers and dates:

// define new culture info
// Provides information about a specific culture (called a locale for unmanaged code development).
// The information includes the names for the culture, the writing system, the calendar used,
// and formatting for dates and sort strings - Source: MSDN
System.Globalization.CultureInfo CINF = new System.Globalization.CultureInfo("");
 
StreamWriter SW = new StreamWriter(Environment.CurrentDirectory + "\\OutputWithCinf.csv");
 
SW.WriteLine("Today's Date;" + DateTime.Today.Date.ToString(CINF));
double DoubleValue = 12.95;
SW.WriteLine("Double Value;" + DoubleValue.ToString(CINF));
 
SW.Close();
SW.Dispose();

Reading a stored file:

// define new culture info
// Provides information about a specific culture (called a locale for unmanaged code development).
// The information includes the names for the culture, the writing system, the calendar used,
// and formatting for dates and sort strings - Source: MSDN
System.Globalization.CultureInfo CINF = new System.Globalization.CultureInfo("");
 
// Variables
DateTime Date = new DateTime();
double DoubleValue = 0;
 
StreamReader SR = new StreamReader(Environment.CurrentDirectory + "_OutputWithCinf.csv");
 
// actual reading of the file
while (SR.Peek() != -1)
{
  // split the string on the semicolon
  string[] stemp = SR.ReadLine().Split(';');
 
  try
  {
    if (stemp[0] == "Today's Date")
    Date = DateTime.Parse(stemp[1],CINF);
    else if (stemp[0] == "Double Value")
    DoubleValue = double.Parse(stemp[1] ,CINF);
  }
    catch (Exception E)
  {
    MessageBox.Show(E.ToString(), "Exception Info", MessageBoxButtons.OK, MessageBoxIcon.Stop);
  }
}

Have a look at the images below for the differences between Windows 7 and Windows 8 for English (South Africa):

Regional Settings South Africa

Regional Settings South Africa - Numbers

Regional Settings South Africa - Date

Source – MSDN

Rudi Coetzee

Rudi Coetzee

Christian. Husband. Free Radical Ginger. Animal Lover. Realist.

You may also like...