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(); 
            }
        }
  • 相关阅读:
    点击回到顶部
    rem根据屏幕大小适配字体大小
    使用layui-tree美化左侧菜单,点击生成tab选项
    字符串
    mysql ORDER BY field(STATUS, 0,1) 根据字段特定排序问题
    js防止短时间内重复点击
    tp5 where a and (b or c)
    array_diff遇到的坑
    前端开发必配置:html5shiv.js和respond.min.js的作用说明!
    phpmailer QQ邮件发送
  • 原文地址:https://www.cnblogs.com/hbreset/p/3092949.html
Copyright © 2011-2022 走看看