Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to set the CultureInfo for an .NET application or just a thread?

Tags:

I've an application written in C# which has no GUI or UI, but instead writes files that are parsed by another application (in XML and others).

I have a customer whose CultureInfo has the NumberDecimalSeparator set to a comma, which causes parsing errors with floating point numbers (PI would end up as 3,1415).

I'd like a way to set the CultureInfo globally within the application, for all threads. I've tried:

  1. The (apparently) customary approach of setting CurrentThread.CurrentCulture as the first line in Main() but it appears to get reset.
  2. A variation/expansion on http://www.codeproject.com/KB/cs/Change_App_Culture.aspx
  3. Do the same (#1) on the explicitly created threads in the application.

And changing to use explicit formatting is not an option (150K+ lines, most written by former employees).

[Edit] The application binds to a socket and handles requests from dedicated clients. Depending on the request type it spawns different handler classes.

Sorry, when I first posted I should have clarified in #1 that (I though) I had done this in all of the handlers that were explicitly spawned, too.

It turns out that I missed the thread/handler that was causing the problem. So the application is working properly now, but the question remains about if the culture can be set on all threads.

If it could iterate over all threads, it would solve the issue, too. So:

How can I get all Thread objects (not ProcessThread) in the current process?

like image 277
NVRAM Avatar asked Feb 10 '10 04:02

NVRAM


People also ask

How do I change the current culture in C#?

CurrentCulture = new CultureInfo("th-TH", false); Console. WriteLine("CurrentCulture is now {0}.", CultureInfo.CurrentCulture.Name); // Display the name of the current UI culture. Console. WriteLine("CurrentUICulture is {0}.", CultureInfo.CurrentUICulture.Name); // Change the current UI culture to ja-JP.

What is the use of CultureInfo in C#?

The CultureInfo class provides culture-specific information, such as the language, sublanguage, country/region, calendar, and conventions associated with a particular culture. This class also provides access to culture-specific instances of the DateTimeFormatInfo, NumberFormatInfo, CompareInfo, and TextInfo objects.

How do I change my UI culture?

To change the current UI culture, you assign the CultureInfo object that represents the new UI culture to the Thread. CurrentThread. CurrentUICulture property.

What is CultureInfo InvariantCulture in C#?

The CultureInfo. InvariantCulture property is used if you are formatting or parsing a string that should be parseable by a piece of software independent of the user's local settings. The default value is CultureInfo. InstalledUICulture so the default CultureInfo is depending on the executing OS's settings.


1 Answers

In .NET 4.5 you can use CultureInfo.DefaultThreadCurrentCulture

like image 61
BPeter Avatar answered Sep 21 '22 14:09

BPeter