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(); 
            }
        }
  • 相关阅读:
    win10安装virtualBox创建CentOS6.5虚拟机
    ES系列二、CentOS7安装ES head6.3.1
    ES系列一、CentOS7安装ES 6.3.1、集成IK分词器
    Python easyGUI 猜数字
    Python easyGUI 登录框 非空验证
    Python easyGUI 文件浏览 显示文件内容
    Python easyGUI 文件对比 覆盖保存
    Python 统计代码量
    什么是一个人真正的魅力?
    Python学习笔记(15)- osos.path 操作文件
  • 原文地址:https://www.cnblogs.com/hbreset/p/3092949.html
Copyright © 2011-2022 走看看