在不同的区域设置中启动程序,CultureInfo [英] Starting program in different regional settings, CultureInfo

查看:80
本文介绍了在不同的区域设置中启动程序,CultureInfo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Guys,



经过一些测试后我又撞到了墙上......

我正在尝试启动一个程序(MS Access)在不同的区域设置中。我试过这样做:

 Thread.CurrentThread.CurrentCulture =  new  CultureInfo(  en-GB); 
Thread.CurrentThread.CurrentUICulture = new CultureInfo( EN-GB);

Access.Application AcApp = new Access.Application();
AcApp.OpenCurrentDatabase( @ C:\ test.accdb);





所以 - 我原来的系统区域设置是德语,所以日期分隔符是点。在en-GB中它应该是/。虽然当我用上面的代码打开数据库时,我仍然看到。作为日期分隔符。 (所以它是2013.04.04,而我想看到2013/04/04)



有人知道我在这里做错了吗?



PS:我在这里想要实现的是让每个人都拥有相同的数据库表视图。无论他们在系统上使用什么区域设置作为默认设置。



非常感谢m8s!

解决方案

< blockquote>首先要说的是:可能,你试图将文化修复到任何格式都会犯错 - 特别是如果它是一个可以混淆的文化。人们将PC上的文化设置为他们习以为常的东西,并将其固定在其他东西上可能会导致问题甚至愤怒。因此,除非这是非常非常好的理由,否则不要这样做。



如果这是有充分理由的话,那就太漂亮了简单易行:我假设您在这里使用DateTime值,因为基于字符串的日期不会改变格式! :笑:



所有你要做的就是停止使用默认转换。如果不知道你正在做什么来显示它,就很难准确地确定你需要做什么,但是在某些时候你正在使用默认的ToString实现,通过字符串连接显式或隐式地使用。相反,请准确指定所需的格式:

 DateTime now = DateTime.Now; 
string s = now.ToString( YYYY / MM / DD);



如果问题是Access中的日期以本地格式显示,那么更改线程文化将无济于事 - Access实例不会与您的应用程序共享一个共同的线程,因为它是一个单独的进程,它有自己的线程集合,因此更改文化对它没有任何影响。如果你开始捣乱全球文化,你将 真的 惹恼你的用户!



有可能根据你用来显示信息的内容来格式化Access本身的特定数据的日期输出。


找到解决方案 - 我编写了从这里读取注册表项的函数:

电脑\ HKEY_CURRENT_USER \控制面板\国际\日期

电脑\ HKEY_CURRENT_USER \控制面板\国际电影日期

将它们的值存储在一些字符串变量中。

将分隔符更改为我需要的值。这立即生效(无需重新登录)

:)

然后我打开数据库,用户看到我想要的东西。

当他关闭程序 - 它恢复设置。



这是迄今为止我能想到的唯一解决方案。


Hello Guys,

After a bit of testing I bumped into wall again...
I am trying to start a program (MS Access) in different regional settings. I have tried to do it this way:

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

Access.Application AcApp = new Access.Application();
AcApp.OpenCurrentDatabase(@"C:\test.accdb");



So - my original regional settings of the system are German so date separator is dot "." and in "en-GB" it should be "/". Though when I open the DB with the code above I still see "." as date separator. (So it''s 2013.04.04, while I want to see 2013/04/04)

Is anyone aware of what I doing wrong here?

P.S.: what I am trying to achieve here is make everyone to have the same view of the database table. No matter what regional settings they are using as default on their systems.

Thanks a lot m8s!

解决方案

The first thing to say is: probably, you are making a mistake by trying to "fix" the culture to any format - particularly if it is one which can be confused. People set the culture on their PC to what they are used to, and fixing it to something else may cause problems or even anger. So unless this is for a very, very good reason, then don''t do it.

If it is for a good reason, then it''s pretty simple to do: I assume that you are using a DateTime value here, as string based dates don''t change format! :laugh:

All you have to do is stop using the default transformation. Without knowning exactly what you are doing to display it, it''s difficult to be precise about what you need to do, ybut at somepoint you are using the default ToString implementation, either explicitly or implicitly via a string concatenation. Instead, specify exactly what format you want:

DateTime now = DateTime.Now;
string s = now.ToString("yyyy/MM/dd");


If the problem is that the dates in Access are being displayed in the local format, then changing the thread culture will not help - the Access instance will not share a common thread with your application because it is a separate Process, with it''s own thread collection, so changing the culture will have no affect on it. And if you start mucking with the global culture you will really annoy your users!

It may be possible to format the date output for specific data in Access itself, depending on what you are using to display the info.


Found the solution - I wrote the function which will read the registry key from here:
Computer\HKEY_CURRENT_USER\Control Panel\International\sDate
Computer\HKEY_CURRENT_USER\Control Panel\International\sShortDate
Store their values in some string variables.
Change the separator to whatever I need. This takes effect immediately (no need to relogin)
:)
Then I open the DB and user sees what I want.
When he closes the program - it restores the settings back.

This is so far the only solution I could think of.


这篇关于在不同的区域设置中启动程序,CultureInfo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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