110,539
社区成员
发帖
与我相关
我的任务
分享
//本代码修改系统时间的显示格式为双位。
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
{
}
}
}
}
[StructLayout (LayoutKind.Sequential)]
public struct SYSTEMTIME
{
public short Year;
public short Month;
public short DayOfWeek;
public short Day;
public short Hour;
public short Minute;
public short Second;
public short Miliseconds;
}
//api函数声明
[DllImport ("kernel32.dll", CharSet=CharSet.Ansi)]
public extern static bool SetSystemTime(ref SYSTEMTIME time);
private void button1_Click(object sender, System.EventArgs e)
{
//调用代码
SYSTEMTIME t = new SYSTEMTIME ();
t.Year = 2000;
t.Month = 1;
t.Day = 2;
t.Hour = 12-8; //这个函数使用的是0时区的时间,对于我们用+8时区的,时间要自己算一下.如要设12点,则为12-8
t.Minute = 5;
bool v = SetSystemTime(ref t);
Console.WriteLine(v.ToString());
}