C# 如何更改 Windows 用户的区域设置/日期格式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1014120/
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
How can I change a Windows user's regional settings/date format?
提问by crb
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.
我使用一个 VB6/COM+ 应用程序,它根据控制面板中的短日期设置为运行它的用户输出日期/时间值,区域设置。然后解析该输出的程序对其期望的日期格式有一个可配置的设置,并在 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".
例如,如果用户的区域设置设置为 mm/dd/yyyy,并且它输出 06/18/2009,则期望“18/06/2009”的应用程序将失败并显示“字符串未被识别为有效的日期时间”。
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.
我想让我为这个混乱编写的 C# 配置实用程序能够以编程方式为给定用户设置日期格式。
EditI 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 amthat 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.
我也知道我所要求的是一件坏事。至于“它应该是用户的选择” -我是用户,我明确地创建它的任务; 我只想通过脚本方法设置日期格式,而不必自己点击。
采纳答案by Stu
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.
回到主题:为应用程序找到一种明确的格式进行通信,例如YYYYMMDD
. 然后显示的应用程序可以简单地尊重实际的用户设置,因为它应该。
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.
sShortDate
可能是您要更改的值。
回答by sean e
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?
如果您要修改配置文件以满足您的需要,为什么不忽略配置文件设置并在您的应用程序中硬编码您想要的格式?
回答by Kavita Mantri
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")
回答by mklement0
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 theSet-Culture
cmdlet, which is the programmatic equivalent of choosing a different region via Control Panel (intl.cpl
).
在 Windows 8 和 Windows Server 2012 及更高版本上,Windows PowerShell 附带Set-Culture
cmdlet,这是通过控制面板 ( intl.cpl
)选择不同区域的编程等效项。
For instance, Set-Culture fr-CA
is the equivalent of interactively choosing region French (Canada)
for the current user.
例如,Set-Culture fr-CA
相当于French (Canada)
为当前用户交互选择区域。
Caveat: Mixedcultures such as en-DE
(sic) appear not to work as of Windows PowerShell v5.1 - see this answerof mine.
警告:混合文化(如en-DE
(sic))从 Windows PowerShell v5.1 开始似乎不起作用 - 请参阅我的这个答案。
While it won't be fast, it is possible to call PowerShell commands from C#.
虽然速度不会很快,但可以从 C# 调用 PowerShell 命令。