哪里系统区域/文化设置的.Net [英] Where is the system locale/culture set for .Net

查看:169
本文介绍了哪里系统区域/文化设置的.Net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#组件(.NET 2.0使用Visual Studio 2005年写的)的问题,即安装在英国的服务器上,并应使用英国区域设置。

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.

我什么code确实是/ MM / YYYY转换日期,格式为DD成UTC。即YYYY-MM-DD。问题出与日期一样16/02/2010,该组件没有将日期转换并返回错误。调试后,我意识到,对于一个奇怪的原因,通过System.CultureInfo返回CultureInfo的是EN-US。

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.

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

I can programatically change those settings using:

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

和我的code正常工作。

and my code works fine.

不过,我并不想这样做,所有的时间,因为我的系统应该是英国。而不是我们。 那么,我该如何改变默认用于.NET Framework是默认的文化EN-GB,而不是EN-US?

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 ?

有关信息:

  • 在我试图更新machine.config文件,并指定文化= EN-GB的全球化部分(它被设定为中性),但它无法正常工作或[已经做到了1.1和2.0],但它是可能的我没有正确的改变它。
  • 在我已验证我的Windows区域设置,他们是绝对设置以英国的日期为DD / MM / YYYY
  • 我在一个虚拟服务器上运行,并验证了我的主机系统。它也被设置为英国

编辑:

额外的细节有关上下文的位。有问题的组件通过从本地C ++第三方组件运行的COM +应用程序COM互操作的调用。

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.

推荐答案

您不必改变的CurrentCulture做改造。如果您确定的日期是DD / MM / YYYY你可以使用

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

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

然后用

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

这样,你会不会有问题,不管的CurrentCulture是什么。但是,如果你不能确定日期的形式为DD / MM / YYYY,而它是基于短期的CurrentCulture日期格式,那么你应该使用

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

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

这篇关于哪里系统区域/文化设置的.Net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆