C# .Net 的系统区域设置/文化设置在哪里

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2288383/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:06:08  来源:igfitidea点击:

Where is the system locale/culture set for .Net

c#.netlocale

提问by andynormancx

I have a problem with a c# assembly (.net 2.0 written using Visual studio 2005) that is installed on a UK server and should use UK regional settings.

我对安装在英国服务器上的 ac# 程序集(使用 Visual Studio 2005 编写的 .net 2.0)有问题,应该使用英国区域设置。

What my code does is to convert a date in the form dd/MM/yyyy into utc. i.e. yyyy-mm-dd. The problem arose with dates like 16/02/2010 where the component failed to convert the date and returned Error. After debugging I realised that, for a strange reason, the CultureInfo returned by System.CultureInfo is en-US.

我的代码所做的是将 dd/MM/yyyy 形式的日期转换为 utc。即 yyyy-mm-dd。问题出现在 16/02/2010 之类的日期中,其中组件未能转换日期并返回错误。调试后我意识到,出于一个奇怪的原因,System.CultureInfo 返回的 CultureInfo 是 en-US。

I can programatically change those settings using:

我可以使用以下方法以编程方式更改这些设置:

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

and my code works fine.

我的代码工作正常。

However I don't want to do that all the time as my system should be UK. Not US. So, how do I change the default culture for .Net framework to be by default en-GB instead of en-US ?

但是我不想一直这样做,因为我的系统应该是英国。不是我们。那么,如何将 .Net 框架的默认文化更改为默认为 en-GB 而不是 en-US ?

For information:

信息:

  • I have tried to update the machine.config file and specify culture=en-GB for the globalization section (it was set to neutral) but it doesn't work either [have done that for 1.1 and 2.0] but it's possible I have not changed it correctly.
  • I have verified my windows regional settings and they are definitely set-up to UK with dates as dd/MM/yyyy
  • I am running in a Virtual server and have verified my host system. It too is set to UK
  • 我试图更新 machine.config 文件并为全球化部分指定culture=en-GB(它被设置为中性)但它也不起作用[已经为1.1 和2.0 这样做了] 但有可能我没有正确地改变了它。
  • 我已经验证了我的 Windows 区域设置,它们肯定设置为英国,日期为 dd/MM/yyyy
  • 我在虚拟服务器中运行并验证了我的主机系统。它也被设置为英国

Edit:

编辑:

A bit of extra detail about the context. The assembly in question is being called via COM interop from a native C++ third party component that is running as a COM+ application.

关于上下文的一些额外细节。有问题的程序集是通过 COM 互操作从作为 COM+ 应用程序运行的本机 C++ 第三方组件调用的。

采纳答案by Nikos Steiakakis

You do not have to change the CurrentCulture to do the transformation. If you are certain that the date is in the form of "dd/MM/yyyy" you could use

您不必更改 CurrentCulture 即可进行转换。如果您确定日期的格式为“dd/MM/yyyy”,则可以使用

DateTime dtTemp = DateTime.ParseExact(dateString, "dd/MM/yyyy", null) // in order not to have to specify a FormatProvider

and then use

然后使用

dtTemp.ToString("yyyy-MM-dd")

This way you will not have a problem no matter what the CurrentCulture is. However, if you are not certain that the Date is of the form "dd/MM/yyyy" rather it is based on the CurrentCulture short date format, then you should use

这样,无论 CurrentCulture 是什么,您都不会遇到问题。但是,如果您不确定日期的格式是“dd/MM/yyyy”,而是基于 CurrentCulture 短日期格式,那么您应该使用

DateTime dtTemp = DateTime(dateString, CurrentCulture.DateTimeFormat.ShortDatePattern, CurrentCulture.DateTimeFormat);

回答by BlueMonkMN

I believe this is represented by System.Globalization.CultureInfo.InstalledUICulture, so if nothing else maybe you can copy that to the thread's current culture. I'm surprised that you found a case where the threads culture is different than the installed culture. Perhaps your code is running in a process that changed the culture?

我相信这是由 System.Globalization.CultureInfo.InstalledUICulture 表示的,所以如果没有别的,也许您可​​以将其复制到线程的当前文化中。我很惊讶您发现线程文化与安装文化不同的情况。也许您的代码正在改变文化的过程中运行?

It is possible the account running the code has different regional settings than the system default. Have you checked that?

运行代码的帐户的区域设置可能与系统默认设置不同。你检查过吗?

回答by garpunkal

To set the UI culture and culture for all pages, add a globalization section to the Web.config file, and then set the uiculture and culture attributes, as shown in the following example:

要为所有页面设置 UI 文化和文化,请将全球化部分添加到 Web.config 文件,然后设置 uiculture 和文化属性,如以下示例所示:

<globalization uiCulture="en" culture="en-GB" />

<globalization uiCulture="en" culture="en-GB" />

回答by Clinton

Hmmm, according to the API Docs:

嗯,根据API 文档

When a thread is started, its culture is initially determined by using GetUserDefaultLCIDfrom the Windows API.

当一个线程启动时,它的文化最初是通过使用GetUserDefaultLCIDWindows API来确定的。

This method derives it's locale from the (as the name implies) User's Default Locale, which I assume is in the Control Panel. NOTE: This is NOT the same as the UI Locale.

此方法从(顾名思义)用户的默认语言环境派生它的语言环境,我假设它在控制面板中。注意:这与 UI 区域设置不同。

回答by shahkalpesh

Assemblies in .net framework are culture neutral.

.net 框架中的程序集是文化中立的。

What code are you trying to convert the date? If you are using Parseor TryParse, try providing the culture argument for it to understand the date.

你想转换日期的代码是什么? 如果您使用ParseTryParse,请尝试为其提供文化参数以了解日期。

回答by Hans Passant

The server is not configured correctly. Control Panel + Region and Language, Location tab. Changing this could be a bit tricky. The server may well have been mis-configured on purpose. Talk to the server administrator first before doing anything.

服务器配置不正确。控制面板 + 区域和语言,位置选项卡。改变这个可能有点棘手。服务器很可能是故意配置错误。在做任何事情之前先与服务器管理员交谈。

Your fallback plan is to use the DateTime.TryParse() method overload that takes the IFormatProvider argument. Pass CultureInfo.GetCultureInfo("en-gb").DateTimeFormat.

您的后备计划是使用采用 IFormatProvider 参数的 DateTime.TryParse() 方法重载。通过 CultureInfo.GetCultureInfo("en-gb").DateTimeFormat。

回答by marc

thanks for your answers (andy posted the question on my behalf). It was indeed an issue with regional settings but neither with the user I was connected under, nor with the user the process was running under. That would have been too easy. It looks like that the default user was still en-US. I did reset by clicking the checkbox "Apply settings to the current user and default user..." in the advanced tab and rebooting the server. System.Globalization.CultureInfo now return {en-GB}. And a MyDate.ToString(yyyy-mm-dd) works fine whether the date is passed as dd/MM/yyyy or dd-MM-yyyy or yyyy-MM-dd without the need to parse.

感谢您的回答(安迪代表我发布了问题)。这确实是区域设置的问题,但与我连接的用户无关,也与运行进程的用户无关。那太容易了。看起来默认用户仍然是 en-US。我通过单击高级选项卡中的复选框“将设置应用于当前用户和默认用户...”并重新启动服务器来重置。System.Globalization.CultureInfo 现在返回 {en-GB}。无论日期是作为 dd/MM/yyyy 还是 dd-MM-yyyy 或 yyyy-MM-dd 传递而无需解析,MyDate.ToString(yyyy-mm-dd) 都可以正常工作。

However thanks you all very much for your suggestions (ParseExact, etc) that did indeed work. They ill be very helpful for other date formats that I was not able to handle in a nice way (yyyyMMdd).

但是,非常感谢大家的建议(ParseExact 等)确实有效。它们对我无法以良好方式处理的其他日期格式非常有帮助 (yyyyMMdd)。

Marc

马克