Click here to Skip to main content
15,881,588 members
Articles / Programming Languages / C#
Article

Custom Global Application Culture

Rate me:
Please Sign up or sign in to vote.
4.71/5 (10 votes)
26 Jul 2007CPOL 70.5K   17   13
This article helps you in defining the application specific culture at the startup time of your application.

Introduction

Many of the times, developers code their application and forget that different systems can have different date and time format settings. This causes the application to crash. This article helps you in setting the Custom global culture info environment specific to your application.

Background

I was working on an existing application and found out the carelessness of the developers. If I changed my system settings, I could not run this application - this issue was assigned to me for fixing. So I did in this way.

Using the Code

The code is self explanatory. Now developers need not worry about the environment settings for the time and date formats.

Set and initialize all the Culture Settings at the start of your application:

C#
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
    // Creating a Global culture specific to our application.
    System.Globalization.CultureInfo cultureInfo = 
		new System.Globalization.CultureInfo("en-US");
    // Creating the DateTime Information specific to our application.
    System.Globalization.DateTimeFormatInfo dateTimeInfo = 
		new System.Globalization.DateTimeFormatInfo();
    // Defining various date and time formats.
    dateTimeInfo.DateSeparator = "/";
    dateTimeInfo.LongDatePattern = "dd-MMM-yyyy";
    dateTimeInfo.ShortDatePattern = "dd-MMM-yy";
    dateTimeInfo.LongTimePattern = "hh:mm:ss tt";
    dateTimeInfo.ShortTimePattern = "hh:mm tt";
    // Setting application wide date time format.
    cultureInfo.DateTimeFormat = dateTimeInfo;
    // Assigning our custom Culture to the application.
    Application.CurrentCulture = cultureInfo;
    Thread.CurrentThread.CurrentCulture = cultureInfo;
    Thread.CurrentThread.CurrentUICulture = cultureInfo;

    ..................
    Application.Run(new Form1());
}

Points of Interest

Well most of you have tried the same thing but maybe it doesn't allow you to change the DateFormats directly. I tried to change the CurrentCulture.DateFormatInfo at various times, but... :)

History

  • 26th July, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader M*Modal
India India
Started carreer with CEERI, Delhi, India as a C Programmer in 2004 and migrated my learning experiance to C#.Net in 2005 with DIGISIGN Noida, Joined Bally in 2006, worked with JK Technosoft since Jun 2007 till Apr 2009, joined back Bally in June 2009 and joined Misys in June 2010. Worked in different domains such as Casino Management, Digital Signage, and Medical and currently working M*Modal as Lead - Product Development.

Comments and Discussions

 
QuestionDoesn't work Pin
Wrangly21-Mar-19 5:29
Wrangly21-Mar-19 5:29 
QuestionNot working for my vs 2008 Pin
M.R SAMEER BAIG3-Mar-15 20:25
M.R SAMEER BAIG3-Mar-15 20:25 
Questionthank you sharing us Pin
4L4K11-Dec-14 7:37
4L4K11-Dec-14 7:37 
QuestionIt didn't work for me in the application level Pin
naga_ptr14-Nov-12 20:36
naga_ptr14-Nov-12 20:36 
It is not working when i use "System.DateTime.Now". Even after i changed the culture, the "Now" returning the current date and time in the system Regional settings format. How to get rid of this. Please help.
SuggestionRe: It didn't work for me in the application level Pin
Mahendra Kumar Srivastava18-Nov-12 20:32
Mahendra Kumar Srivastava18-Nov-12 20:32 
GeneralExcellent Pin
Dipti Mamidala30-Jul-12 19:30
Dipti Mamidala30-Jul-12 19:30 
QuestionMy vote of 5 Pin
eka80822-May-12 2:21
eka80822-May-12 2:21 
GeneralGood Pin
krgsoft24-Apr-12 2:42
krgsoft24-Apr-12 2:42 
GeneralGood Pin
Muhammad Ghufran17-Feb-12 0:51
Muhammad Ghufran17-Feb-12 0:51 
Generalgood Pin
pluniewski21-Apr-09 0:26
pluniewski21-Apr-09 0:26 
GeneralRe: good Pin
Mahendra Kumar Srivastava17-Jul-09 0:18
Mahendra Kumar Srivastava17-Jul-09 0:18 
Generalexcellent Pin
anand.lv30-Nov-08 22:23
anand.lv30-Nov-08 22:23 
GeneralRe: excellent Pin
Mahendra Kumar Srivastava17-Jul-09 0:19
Mahendra Kumar Srivastava17-Jul-09 0:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.