123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- using System;
- using System.Collections.Generic;
- using System.Runtime.InteropServices;
- using System.Text;
- namespace LYFZ.WinAPI
- {
- public class SetComputerDateTime
- {
- [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;
- [DllImportAttribute("Kernel32.dll")]
- public static extern void SetLocalTime(SystemTime st);
- //public ushort wYear;
- //public ushort wMonth;
- //public ushort wDayOfWeek;
- //public ushort wDay;
- //public ushort wHour;
- //public ushort wMinute;
- //public ushort wSecond;
- //public ushort wMilliseconds;
- public SetComputerDateTime()
- {
- }
- /// <summary>
- /// 设置系统时间格式
- /// </summary>
- public static void SetDateTimeFormat()
- {
- try
- {
- int x = GetSystemDefaultLCID();
- SetLocaleInfo(x, LOCALE_SSHORTDATE, "yyyy-MM-dd"); //短日期格式
- SetLocaleInfo(x, LOCALE_SLONGDATE, "yyyy年MM月dd日"); //长日期格式
- SetLocaleInfo(x, LOCALE_STIME, "HH:mm:ss"); //时间格式
- }
- catch
- {
-
- }
- }
- /// <summary>
- /// 设置系统时间
- /// </summary>
- /// <param name="dateTime"></param>
- public static void SetDateTime(DateTime dateTime)
- {
- try
- {
- SystemTime mySystemTime = new SystemTime();
- mySystemTime.wYear = (ushort)dateTime.Year;//年
- mySystemTime.wMonth = (ushort)dateTime.Month;//月
- mySystemTime.wDay = (ushort)dateTime.Day;//日
- mySystemTime.wHour = (ushort)dateTime.Hour;//时
- mySystemTime.wMinute = (ushort)dateTime.Minute;//分
- mySystemTime.wSecond = (ushort)dateTime.Second;//秒
- SetLocalTime(mySystemTime);
- }
- catch
- {
-
- }
- }
- }
- [StructLayoutAttribute(LayoutKind.Sequential)]
- public class SystemTime
- {
- public ushort wYear;
- public ushort wMonth;
- public ushort wDayOfWeek;
- public ushort wDay;
- public ushort wHour;
- public ushort wMinute;
- public ushort wSecond;
- public ushort wMilliseconds;
- }
- }
|