zoukankan      html  css  js  c++  java
  • 修改操作系统时间

        internal class LocalTimeHelper
        {
            [StructLayout(LayoutKind.Sequential)]
            struct SystemTime
            {
                public UInt16 Year;
                public UInt16 Month;
                public UInt16 DayOfWeek;
                public UInt16 Day;
                public UInt16 Hour;
                public UInt16 Minute;
                public UInt16 Second;
                public UInt16 Millisecond;
    
                public DateTime ToDateTime()
                {
                    return new DateTime(Year, Month, Day, Hour, Minute, Second, Millisecond);
                }
    
                public SystemTime FromDateTime(DateTime value)
                {
                    Year = (UInt16)value.Year;
                    Month = (UInt16)value.Month;
                    Day = (UInt16)value.Day;
                    Hour = (UInt16)value.Hour;
                    Minute = (UInt16)value.Minute;
                    Second = (UInt16)value.Second;
                    Millisecond = (UInt16)value.Millisecond;
    
                    return this;
                }
    
                public static implicit operator DateTime(SystemTime value)
                {
                    return value.ToDateTime();
                }
    
                public static implicit operator SystemTime(DateTime value)
                {
                    return new SystemTime().FromDateTime(value);
                }
            }
    
            [DllImport("kernel32.dll")]
            static extern int SetSystemTime(ref SystemTime systemTime);
            [DllImport("Kernel32.dll")]
            static extern void GetSystemTime(ref SystemTime sysTime);
    
            public static bool SetSystemLocalTime(DateTime value)
            {
                SystemTime time = value.ToUniversalTime();
                var res = SetSystemTime(ref time);
                return res != 0;
            }
    
            public static DateTime GetSystemLocalTime()
            {
                var time = new SystemTime();
                GetSystemTime(ref time);
                DateTime res = ((DateTime)time).ToLocalTime();
    
                return res;
            }
        }
  • 相关阅读:
    刷题记录:[ByteCTF 2019]EZCMS
    angularJS——自定义指令
    HTML5——语音输入
    jQuery表单验证插件——jquery.validate.js
    纯CSS气泡效果
    管理Cookie的插件——jquery.cookie.js
    网页打印
    CSS hack
    如何挑选适合的前端框架(去哪儿网前端架构师司徒正美)
    让DIV水平和垂直居中的几种方法
  • 原文地址:https://www.cnblogs.com/baiqjh/p/4244680.html
Copyright © 2011-2022 走看看