zoukankan      html  css  js  c++  java
  • 改变系统时间c#

        //两种方法,但都需要 run as admin
        [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;
        }
        public static class SetOSDateTime
        {
            [DllImport("Kernel32.dll")]
            public static extern bool SetLocalTime(ref SystemTime sysTime);
            /// <summary>
            /// 
            /// </summary>
            /// <param name="time">2013-11-18</param>
            /// <returns></returns>
            public static bool SetByAPI(string time)
            {
                bool flag = false;
                SystemTime sysTime = new SystemTime();
                DateTime dt = Convert.ToDateTime(time);
                sysTime.wYear = Convert.ToUInt16(dt.Year);
                sysTime.wMonth = Convert.ToUInt16(dt.Month);
                sysTime.wDay = Convert.ToUInt16(dt.Day);
                sysTime.wHour = Convert.ToUInt16(dt.Hour);
                sysTime.wMinute = Convert.ToUInt16(dt.Minute);
                sysTime.wSecond = Convert.ToUInt16(dt.Second);
                try
                {
                    flag = SetOSDateTime.SetLocalTime(ref sysTime);
                }
                catch (Exception e)
                {
                    Console.WriteLine("SetSystemDateTime函数执行异常" + e.Message);
                }
                return flag;
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="time">02-20-2014</param>
            public static void SetByProcess(string time)
            {
                Process p = new Process();
                p.StartInfo.FileName = "cmd.exe";
                p.StartInfo.Arguments = "/c date 02-20-2014";
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = true;
                p.StartInfo.CreateNoWindow = true;
                p.Start();
                //return p.StandardOutput.ReadToEnd(); 
            }
        }
  • 相关阅读:
    Object.assign()方法
    JavaScript对象(二)
    JavaScript对象(一)
    vue开发中遇到的问题集锦(2)
    Python:str.ljust()、str.rjust()、str.center()函数
    Python:如何将多个小字符串拼接成一个大的字符串
    Python:.join()函数
    Python:生成器表达式
    VS Code:快捷方式
    Python:如何调整字符串中文本的格式
  • 原文地址:https://www.cnblogs.com/hbreset/p/3092949.html
Copyright © 2011-2022 走看看