如何更改 Windows 用户的区域设置/日期格式?

发布于 2024-07-25 02:59:48 字数 525 浏览 9 评论 0原文

我使用 VB6/COM+ 应用程序,该应用程序根据运行该应用程序的用户的“控制面板”、“区域设置”中的短日期设置输出日期/时间值。 然后解析该输出的程序具有其期望的日期格式的可配置设置,并显示在 UI 中。

例如,如果用户的区域设置设置为 mm/dd/yyyy,并且输出 06/18/2009,则期望“18/06/2009”的应用程序将失败,并显示“字符串未被识别为有效的日期时间”。

由于我们通常作为服务帐户运行此应用程序,而我们没有以交互方式登录来创建配置文件,因此我们通常设置正确的日期格式,然后勾选“将所有设置应用到当前用户帐户和默认用户配置文件”选项。

我希望我为这个混乱编写的 C# 配置实用程序能够以编程方式为给定用户设置日期格式。

编辑 我只想更改代码,但目前没有能力这样做。

我也知道我所要求的是一件坏事。 关于“它应该是用户的选择” - 我那个用户,因为我明确地为任务创建了它; 我只想通过脚本方法设置日期格式,而不是自己点击。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

疾风者 2024-08-01 02:59:48

Microsoft 特别不鼓励这样做。 您可能想出的任何解决方案都将是一个肮脏的黑客,可能很快就会停止工作。

这样想:你是谁来决定这些设置? 你不认为这是用户的决定吗?

回到主题:找到一种明确的格式供应用程序进行通信,例如 YYYYMMDD。 然后,显示的应用程序可以简单地尊重实际的用户设置,就像它应该的那样。

但是,由于您无法更改它,只需查看注册表:

当前用户:

HKEY_CURRENT_USER\Control Panel\International

特定用户:

HKEY_USERS\(user SID)\Control Panel\International

默认用户:

HKEY_USERS\.DEFAULT\Control Panel\International

sShortDate 可能是您想要更改的值。

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.

余厌 2024-08-01 02:59:48

如果您要修改配置文件以满足您的需求,为什么不忽略配置文件设置并在应用程序中硬编码您想要的格式呢?

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?

热情消退 2024-08-01 02:59:48

代码:

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")

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")
秋凉 2024-08-01 02:59:48

虽然持续改变用户的文化(区域设置)需要谨慎进行,但也有合法的用例。

在 Windows 8 和 Windows Server 2012 及更高版本上,Windows PowerShell 附带
Set-Culture cmdlet,即相当于通过控制面板 (intl.cpl) 选择不同区域的编程方式。

例如,Set-Culture fr-CA 相当于为当前用户交互式选择地区法语(加拿大)

警告:从 Windows PowerShell v5.1 开始,en-DE(原文如此)等混合文化似乎无法正常工作 - 请参阅 我的这个答案

虽然速度不会很快,但可以从 C# 调用 PowerShell 命令。

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#.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文