C# 是否可以为 .NET 应用程序或只是一个线程设置 CultureInfo?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2234492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to set the CultureInfo for an .NET application or just a thread?
提问by NVRAM
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).
我有一个用 C# 编写的应用程序,它没有 GUI 或 UI,而是编写由另一个应用程序(XML 和其他应用程序)解析的文件。
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).
我有一个客户的 CultureInfo 将 NumberDecimalSeparator 设置为逗号,这会导致浮点数解析错误(PI 最终为3,1415)。
I'd like a way to set the CultureInfo globally within the application, for all threads. I've tried:
我想要一种在应用程序中为所有线程全局设置 CultureInfo 的方法。我试过了:
- The (apparently) customary approach of setting CurrentThread.CurrentCultureas the first line in Main()but it appears to get reset.
- A variation/expansion on http://www.codeproject.com/KB/cs/Change_App_Culture.aspx
- Do the same (#1) on the explicitly created threads in the application.
- 将CurrentThread.CurrentCulture设置为Main() 中第一行的(显然)习惯方法,但它似乎被重置了。
- http://www.codeproject.com/KB/cs/Change_App_Culture.aspx上的变体/扩展
- 在应用程序中显式创建的线程上执行相同的操作 (#1)。
And changing to use explicit formatting is not an option (150K+ lines, most written by former employees).
并且更改为使用显式格式不是一种选择(150K+ 行,大部分由前员工编写)。
[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.
抱歉,当我第一次发布时,我应该在 #1 中澄清(我虽然)在所有显式生成的处理程序中也这样做了。
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 Threadobjects (not ProcessThread) in the current process?
如何获取当前进程中的所有Thread对象(不是ProcessThread)?
采纳答案by Jon Seigel
Unfortunately, every new thread starts with system locale information, even if it's started from a thread that's had its locale changed to something else.
不幸的是,每个新线程都以系统区域设置信息开始,即使它是从将其区域设置更改为其他内容的线程启动的。
This was a huge gotcha I ran into in one of our apps when using a BackgroundWorker
to load a file.
这是我在使用 aBackgroundWorker
加载文件时在我们的一个应用程序中遇到的一个巨大问题。
The approach I've used successfully is to set the locale on the startup thread, and then use a thread factory to create threads with the "application locale." For BackgroundWorkers you can use either a factory or a derived class, as Thread
is sealed while BackgroundWorker
is not.
我成功使用的方法是在启动线程上设置语言环境,然后使用线程工厂创建具有“应用程序语言环境”的线程。对于 BackgroundWorkers,您可以使用工厂或派生类,因为Thread
是密封的,而BackgroundWorker
不是。
回答by Paolo Tedesco
I don't think you can set the culture for the whole application, but you can set the culture explicilty whenever you create a thread:
我不认为您可以为整个应用程序设置文化,但是您可以在创建线程时设置文化显式:
using System;
using System.Globalization;
using System.Threading;
class Program {
static void thread_test() {
Console.WriteLine("Culture: {0}", CultureInfo.CurrentCulture.DisplayName);
}
public static void Main(params string[] args) {
Thread t = new Thread(thread_test);
t.CurrentCulture = new CultureInfo("it-it");
t.Start();
t.Join();
}
}
回答by friuns
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
回答by BPeter
In .NET 4.5 you can use CultureInfo.DefaultThreadCurrentCulture
在 .NET 4.5 中,您可以使用 CultureInfo.DefaultThreadCurrentCulture