简体   繁体   English

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

[英]Where is the system locale/culture set for .Net

I have a problem with ac# assembly (.net 2.0 written using Visual studio 2005) that is installed on a UK server and should use UK regional settings. 我有一个安装在英国服务器上的ac#assembly(使用Visual Studio 2005编写的.net 2.0)的问题,应该使用英国区域设置。

What my code does is to convert a date in the form dd/MM/yyyy into utc. 我的代码所做的是将dd / MM / yyyy格式的日期转换为utc。 ie yyyy-mm-dd. 即yyyy-mm-dd。 The problem arose with dates like 16/02/2010 where the component failed to convert the date and returned Error. 问题出现在16/02/2010等日期,组件无法转换日期并返回错误。 After debugging I realised that, for a strange reason, the CultureInfo returned by System.CultureInfo is en-US. 调试后我意识到,由于一个奇怪的原因,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. 我试图更新machine.config文件并为全球化部分指定culture = en-GB(它被设置为中性)但它不起作用[已经为1.1和2.0做了]但是我可能没有改变了它。
  • I have verified my windows regional settings and they are definitely set-up to UK with dates as dd/MM/yyyy 我已经验证了我的Windows区域设置,它们肯定是设置为英国,日期为dd / MM / yyyy
  • I am running in a Virtual server and have verified my host system. 我在虚拟服务器上运行并验证了我的主机系统。 It too is set to UK 它也被设定为英国

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 interop从作为COM +应用程序运行的本机C ++第三方组件调用的。

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. 您的后备计划是使用带有IFormatProvider参数的DateTime.TryParse()方法重载。 Pass CultureInfo.GetCultureInfo("en-gb").DateTimeFormat. 传递CultureInfo.GetCultureInfo(“en-gb”)。DateTimeFormat。

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

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

Hmmm, according to the API Docs : 嗯,根据API文档

When a thread is started, its culture is initially determined by using GetUserDefaultLCID from the Windows API. 启动线程时,最初使用Windows API中的GetUserDefaultLCID确定其文化。

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区域设置不同。

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. 看起来默认用户仍然是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}. System.Globalization.CultureInfo现在返回{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. 无论日期是以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. 但是非常感谢你们的建议(ParseExact等)确实有效。 They ill be very helpful for other date formats that I was not able to handle in a nice way (yyyyMMdd). 它们对我无法以一种很好的方式处理的其他日期格式非常有帮助(yyyyMMdd)。

Marc

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. 我相信这是由System.Globalization.CultureInfo.InstalledUICulture表示的,所以如果没有别的可能你可以将它复制到线程的当前文化中。 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? 也许您的代码在改变文化的过程中运行?

It is possible the account running the code has different regional settings than the system default. 运行代码的帐户可能具有与系统默认值不同的区域设置。 Have you checked that? 你检查过了吗?

You do not have to change the CurrentCulture to do the transformation. 您无需更改CurrentCulture即可进行转换。 If you are certain that the date is in the form of "dd/MM/yyyy" you could use 如果您确定日期是“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. 这样,无论CurrentCulture是什么,您都不会有任何问题。 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 但是,如果您不确定日期是“dd / MM / yyyy”形式,而是基于CurrentCulture短日期格式,那么您应该使用

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

Assemblies in .net framework are culture neutral. .net框架中的程序集是文化中立的。

What code are you trying to convert the date? 你想用什么代码转换日期? If you are using Parse or TryParse , try providing the culture argument for it to understand the date. 如果您正在使用 ParseTryParse ,请尝试为其提供culture参数以了解日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM