How to define the locale of an application in .NET

2019-08-28 21:50发布

问题:

I have an application developted with Visual C++ 2008 Express Edition under Windwos XP, which runs propertly on one computer, where the default langaunge is set to English. However, if run the same application on a different computer with default language German, I run into troubles because a predefined string Infinity is not recoginzed during conversion to double using ToDouble, because on the German platform the string should be Unendlich. In particular the mscorlib throws correctly a FormatException.

How can I force the application to run with the English locale? I could not yet find any option...

Thanks for any hint.

回答1:

Use Convert::ToDouble(str, System::Globalization::CultureInfo::InvariantCulture);



回答2:

You can set the current locale to English with:

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US", false);

That will force US English in the current thread. If you do that in the main Windows Forms thread, then your UI culture will always be English. See http://msdn.microsoft.com/en-us/library/b28bx3bh(v=VS.100).aspx for more info.

As Hans pointed out in his comment, this doesn't affect pool threads and can lead to some hard to find bugs. It turns out that there is no global setting that will make every thread use the culture that you define. If you want that functionality, you'll have to make your own application-wide setting and ensure that all threads use it.

Also good to read would be Globalizing Windows Forms.



回答3:

Convert.ToDouble(stringValue, System.Globalization.CultureInfo.InvariantCulture);