C#更改系统日期时间格式

C#更改系统日期时间格式,c#,C#,我正在寻找一种方法来改变我的系统的日期时间格式从12小时到24小时。假设,目前,我有12小时或24小时格式的系统时间,我想通过编程将其更改为24小时格式。怎么可能呢 谢谢您可以使用API函数,将常量作为LCType参数传递 编辑: 下面是设置12小时时间格式的示例 using System; using System.Runtime.InteropServices; namespace DllImportTest { internal static class Program

我正在寻找一种方法来改变我的系统的日期时间格式从12小时到24小时。假设,目前,我有12小时或24小时格式的系统时间,我想通过编程将其更改为24小时格式。怎么可能呢

谢谢

您可以使用API函数,将常量作为
LCType
参数传递

编辑:

下面是设置12小时时间格式的示例

using System;
using System.Runtime.InteropServices;

namespace DllImportTest
{
    internal static class Program
    {
        private const string TimeFormat12Hour = "0";
        private const string TimeFormat24Hour = "1";

        public static void Main(string[] args)
        {
            var ok = WindowsApi.SetLocaleInfo(0, WindowsApi.LOCALE_ITIME, TimeFormat12Hour);
            if (!ok)
                throw new ApplicationException(string.Format("Windows API call error {0}.", Marshal.GetLastWin32Error()));
        }
    }

    internal static class WindowsApi
    {
        public const int LOCALE_ITIME = 0x00000023;

        [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        public static extern bool SetLocaleInfo(uint locale, uint lcType, string lcData);
    }
}

请注意,文档中指出,在对系统参数进行国际更改后,有必要向所有顶级窗口广播
WM_SETTINGCHANGE
消息。这样做需要使用另一个Windows API函数。此部分在示例中被省略。

您需要批处理文件解决方案还是C#解决方案?我正在寻找C#解决方案,因为我的实际程序是C#。您需要PInvoke访问windows设置。然后您必须PInvoke下面的答案中提供的windows API函数。如果你不知道如何使用PInvoke,请查看关于如何使用PInvoke的无数教程之一:为什么要使用PInvoke?您真的想为系统或应用程序更改它吗?添加了一个简单的示例。