隐藏

修改系统时间的显示格式为双位C#源代码

发布:2020/5/14 10:12:43作者:管理员 来源:本站 浏览次数:916

//本代码修改系统时间的显示格式为双位。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.Win32;

namespace SetSystermTime
{
    public class SetSystermTime
    {
        [DllImport("kernel32.dll", EntryPoint = "GetSystemDefaultLCID")]
        public static extern int GetSystemDefaultLCID();
        [DllImport("kernel32.dll", EntryPoint = "SetLocaleInfoA")]
        public static extern int SetLocaleInfo(int Locale, int LCType, string lpLCData);

        public const int LOCALE_SLONGDATE = 0x20;
        public const int LOCALE_SSHORTDATE = 0x1F;
        public const int LOCALE_STIME = 0x1E;

        public static void SetTimeForMat()
        {
            try
            {
                //读写注册表
                RegistryKey ST = Registry.CurrentUser;
                RegistryKey ST1 = ST.OpenSubKey("Control Panel\\International", true);
                if (ST1 == null)
                {
                    // ST1.CreateSubKey("sTimeFormat");
                    ST1.SetValue("sTimeFormat", "HH:mm:ss");
                }
                else
                {
                    ST1.SetValue("sTimeFormat", "HH:mm:ss");
                }


                int x = GetSystemDefaultLCID();
                SetLocaleInfo(x, LOCALE_SSHORTDATE, "yyyy-MM-dd");   //短日期格式  
                SetLocaleInfo(x, LOCALE_SLONGDATE, "yyyy年MM月dd日");   //长日期格式
                SetLocaleInfo(x, LOCALE_STIME, "HH:mm:ss tt");        //时间格式
            }
            catch
            {

            }
        }

    }
}

 

运行一下再看看你的系统时间是不是变过来了?



然后可以在全局Global.asax里引用包<%@ Import Namespace="SetSystermTime" %>,并在应用Application_Start启动时里添加SetSystermTime.SetTimeForMat();