读写系统时间

[StructLayout(LayoutKind.Sequential)]
        public struct SystemTime
        {
            public ushort wYear;
            public ushort wMonth;
            public ushort wDayOfWeek;
            public ushort wDay;
            public ushort wHour;
            public ushort wMinute;
            public ushort wSecond;
            public ushort wMiliseconds;
        }
        [DllImport("Kernel32.dll")]
        public static extern bool SetSystemTime(ref SystemTime sysTime);
        [DllImport("Kernel32.dll")]
        public static extern void GetSystemTime(ref SystemTime sysTime);

        public static void Set(DateTime dateTime)
        {
            DateTime dtUnivr = dateTime.ToUniversalTime();
            SystemTime sysTime = new SystemTime();
            sysTime.wYear = (ushort)dtUnivr.Year;
            sysTime.wMonth = (ushort)dtUnivr.Month;
            sysTime.wDay = (ushort)dtUnivr.Day;
            sysTime.wHour = (ushort)dtUnivr.Hour;
            sysTime.wMinute = (ushort)dtUnivr.Minute;
            sysTime.wSecond = (ushort)dtUnivr.Second;
            sysTime.wMiliseconds = (ushort)dtUnivr.Millisecond;
            SetSystemTime(ref sysTime);
        }
        public static void Set(int Year, int Month, int Day, int Hour, int Minute, int Second)
        {
            Set(new DateTime(Year, Month, Day, Hour, Minute, Second));
        }

 

posted on 2013-11-15 17:26  筑梦1582  阅读(138)  评论(0编辑  收藏  举报