【发布时间】:2012-10-18 10:04:49
【问题描述】:
我需要以编程方式永久更改/更新/设置我的系统 (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 + "");
}
}
}
【问题讨论】:
-
看看这个 SO 帖子*.com/questions/650849/…
-
感谢@CodeIgnoto。但我认为是设置
Date而不是Date Format或DateTime Format -
试试 System.IFormatProvider Interface.visit : www1.cs.columbia.edu/~lok/csharp/refdocs/System/types/…
-
@WajidAli Nai jani。需要 SetFormat 而不是 GetFormat。我也期待一些例子。因为我也一直在寻找很多这样的东西两个多小时。但没有什么是真正有用的:(
-
@Sami:我已经为您的问题做了很多研发,但没有找到任何代码 sn-p。
标签: c# winforms datetime datetime-format