1

我需要以编程方式永久更改/更新/设置我的系统(windows7)默认日期时间格式,直到我自己通过代码或通过 Windows GUI 更改它

我已经尝试了很多像这样来自Code Project的解决方案

       Console.Write(DateTime.Now + "");
       RegistryKey rkey = Registry.CurrentUser.OpenSubKey(@"
       Control Panel\International", true);
        rkey.SetValue("sShortDate", "dd/MM/yyyy");
        rkey.SetValue("sLongDate", "dd/MM/yyyy");
       Console.Write(DateTime.Now + "");

最接近且享有盛誉的答案来自Set Default DateTime Format c#,我尝试了此解决方案,但对我没有帮助。因为它不会改变我的系统日期时间格式(显示在任务栏中)。在我重新启动具有此代码的应用程序后,我在执行此代码之前再次获得旧格式。

using System;
using System.Globalization;
using System.Threading;

namespace test 
{
    public static class Program
    {
        public static void Main() {
          Console.Write(DateTime.Now + "");// MessageBox.Show(DateTime.Now + "");
          CultureInfo culture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
          culture.DateTimeFormat.ShortDatePattern = "dd-MMM-yyyy";
          culture.DateTimeFormat.LongTimePattern = "";
          Thread.CurrentThread.CurrentCulture = culture;
          Console.Write(DateTime.Now + "");// MessageBox.Show(DateTime.Now + "");
        }
    }
}
4

1 回答 1

1

我尝试手动更改您的问题中提到的注册表项。

这是我所做的:

  • Win+R并键入“regedit”以获取注册表编辑器。
  • Navigated the tree to Computer → HKEY_CURRENT_USER → Control Panel → International.
  • Double clicked the 'sShortDate' entry and changed the value to 'yyyy/MM/dd'.

I then opened Windows Explorer and all of the dates were shown in the new format, so this is definitely the right place in the registry.

I then tried the code you supplied to modify the registry and it, too, is changing the date shown in Explorer.

So, this leads me to believe that the Windows taskbar clock does not react to changes to this setting. I confirmed this by killing and restarting 'explorer.exe' from the Task Manager. If you restart Explorer you too should see the change take effect.


Edit: there appears to be no facility in .NET for setting the locale settings directly. You can, however, use P/Invoke to set it via the C++ Win API which should then cause the system clock (and other applications) to be notified of the change. See this discussion.

于 2012-10-18T10:36:22.317 回答