How can I change a Windows user's regional settings/date format?

拜拜、爱过 提交于 2019-11-30 09:21:06

问题


I use a VB6/COM+ application which outputs date/time values based on the short date settings in the Control Panel, Regional Settings, for the user that runs it. The program that then parses that output has a configurable setting for the date format it expects, and presents in the UI.

e.g. If the regional setting for the user is set to mm/dd/yyyy, and it outputs 06/18/2009, the application expecting "18/06/2009" fails with "String was not recognized as a valid DateTime".

As we usually run this application as a service account, which we have not logged in as interactively to create a profile, we generally set the correct date format and then tick the "Apply all settings to the current user account and the default user profile" option.

I would like to be make the C# configuration utility I have written for this mess to be able to set the date format programmatically for a given user.

Edit I would like nothing more than to change the code, but do not have the ability to do so at this time.

I also know that what I am asking is a bad thing to do. With regards to "it should be the user's choice" - I am that user, as I create it explicitly for the task; I just want to set the date format by a scripted method, rather than having to do the clicking myself.


回答1:


This is specifically discouraged by Microsoft. Any solution you may come up with will be a filthy hack that will probably stop working soon.

Think of it this way: who are you to decide those settings? Don't you think that's the user's decision?

Back on topic: find an unambiguous format for the applications to communicate in, such as YYYYMMDD. The application that displays can then simply respect the actual user settings, as it should.

But, since you can't change it, just poke into the registry:

Current user:

HKEY_CURRENT_USER\Control Panel\International

Specific user:

HKEY_USERS\(user SID)\Control Panel\International

Default user:

HKEY_USERS\.DEFAULT\Control Panel\International

sShortDate is probably the value you want to change.




回答2:


If you are going to modify the profile to suit your needs, why not just ignore the profile settings and hardcode the format you want in your app?




回答3:


code:

Imports System.Threading
Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US", False)
Microsoft.Win32.Registry.SetValue("HKEY_CURRENT_USER\Control Panel\International", "sShortDate", "M/d/yyyy")



回答4:


While persistently changing a user's culture (regional settings) is to be done cautiously, there are legitimate use cases.

On Windows 8 and Windows Server 2012 and above, Windows PowerShell comes with the
Set-Culture cmdlet, which is the programmatic equivalent of choosing a different region via Control Panel (intl.cpl).

For instance, Set-Culture fr-CA is the equivalent of interactively choosing region French (Canada) for the current user.

Caveat: Mixed cultures such as en-DE (sic) appear not to work as of Windows PowerShell v5.1 - see this answer of mine.

While it won't be fast, it is possible to call PowerShell commands from C#.



来源:https://stackoverflow.com/questions/1014120/how-can-i-change-a-windows-users-regional-settings-date-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!