问题描述
在一个有趣的情况下,语言环境设置与我的C#应用程序发生了混乱,因为我未能意识到像Double.Parse这样的方法不会将"1000"转换为1000,但是由于编号格式的不同,会做一些意想不到的事情
I've got an interesting situation where locale settings has messed with my C# application, because I failed to realize that methods like Double.Parse will not convert "1000" to 1000, but do something unexpected due to the different numbering format.
解决我的问题的一种方法是使用类似 double d = double.parse("1000",new CultureInfo("en-US"));
之类的东西.目前,我没有通过CultureInfo.但是,我想知道是否有可能在启动时影响只是我的应用程序的语言环境.
One solution to my problem would be to use something like double d = double.parse( "1000", new CultureInfo("en-US"));
. Currently, I don't pass the CultureInfo. However, instead of having to make this change throughout the code, I was wondering if it's possible to affect the locale of just my application upon startup.
我在MSDN上找到了文章表示我可以使用以下代码实现这一点:
I have found an article on MSDN that says I can achieve this with the following code:
using System.Threading;
using System.Globalization;
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
但是并没有说从主线程产生的工作线程是否也将继承父级的文化.
But it doesn't say whether or not worker threads spawned from the main thread will also inherit the parent's culture.
我认为并非如此,因为在.NET 4.5中,显然存在一个 new CultureInfo.DefaultThreadCurrentCulture
属性,该属性指定应用程序域中所有线程的区域性,但是.NET 4.0中没有这样的东西.
I assume this is not the case, as in .NET 4.5 there is apparently a new CultureInfo.DefaultThreadCurrentCulture
property that specifies the culture for all threads in the app domain, but there isn't anything like this in .NET 4.0.
有人可以为这个地区问题推荐一个好的解决方案吗?
Can anyone recommend a nice solution for this locale issue?
推荐答案
我最终只是尝试了一下.在.NET 4.0中,新线程不会不继承从其创建线程的语言环境.我确实根据MSDN文档设置了 CurrentCulture
和 CurrentUICulture
,并且昨天通过了所有测试,所以这是一件好事.
I ended up just trying it out. In .NET 4.0, new threads do not inherit the locale of the thread they are created from. I did set CurrentCulture
and CurrentUICulture
per the MSDN documentation and all of the tests yesterday passed, so that's a good thing.
这篇关于更改应用程序的语言环境而不更改Windows的语言环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!