C#修改系统时间

main:

 

class Program
    {
        static string input;
        static UpdateHelper updTime;
        static void Main(string[] args)
        {
            Console.WriteLine("START APPLICATION!");
            //获取当前时间
            DateTime today = System.DateTime.Now;
            //要变更的时间
            DateTime beforeDate = new DateTime(2010,01,01);
            updTime = new UpdateHelper();
            bool res = updTime.UpdateDateTime(beforeDate);
            Console.WriteLine(beforeDate.ToString());
           
            //调用lingoes
            Process myProcess = new Process();
            try
            {
                myProcess.StartInfo.UseShellExecute = false;
                myProcess.StartInfo.FileName = @"D:\tools\Translator\Lingoes.exe";
                myProcess.StartInfo.CreateNoWindow = true;
                myProcess.Start(); 
            }
            catch(Exception e) {
                Console.WriteLine("系统找不到指定的程序文件。\r{0}", e);
                return;
            }

            //休眠
            System.Threading.Thread.Sleep(3000);

            //还原当前时间
            updTime = new UpdateHelper();
            bool res1 = updTime.UpdateDateTime(today);
            Console.WriteLine(today.ToString());

            //END
            Console.WriteLine("入力Enter終了!");
            input = Console.In.ReadLine();
            //System.Console.WriteLine(input);
            }
    }

 

updateDate:

public class UpdateHelper
    {
        [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 wMilliseconds;
            //设置系统时间参数
            public void FromDateTime(DateTime dateTime)
            {
                wYear = (ushort)dateTime.Year;
                wMonth = (ushort)dateTime.Month;
                wDayOfWeek = (ushort)dateTime.DayOfWeek;
                wDay = (ushort)dateTime.Day;
                wHour = (ushort)dateTime.Hour;
                wMinute = (ushort)dateTime.Minute;
                wSecond = (ushort)dateTime.Second;
                wMilliseconds = (ushort)dateTime.Millisecond;
            }
            //获取系统时间
            public DateTime ToDateTime()
            {
                return new DateTime(wYear, wMonth, wDay, wHour, wMinute, wSecond);
            }
        }
        //设定,获取系统时间,SetSystemTime()默认设置的为UTC时间,比北京时间少了8个小时因此我们只用LocalTime。  
        [DllImport("Kernel32.dll")]
        public static extern bool SetSystemTime(ref SYSTEMTIME time);
        [DllImport("Kernel32.dll")]
        public static extern bool SetLocalTime(ref SYSTEMTIME time);
        [DllImport("Kernel32.dll")]
        public static extern void GetSystemTime(ref SYSTEMTIME time);
        [DllImport("Kernel32.dll")]
        public static extern void GetLocalTime(ref SYSTEMTIME time);
        /// <summary>
        /// 修改系统日期时间 
        /// </summary>
        /// <param name="datetime">新日期时间</param>
        /// <returns>修改是否成功</returns>
        public bool UpdateDateTime(DateTime datetime) //修改系统时间
        {
            SYSTEMTIME st = new SYSTEMTIME();
            st.FromDateTime(datetime);
            bool res = SetLocalTime(ref st);
            return res;
            /*COleDateTime tm;
            SYSTEMTIME st;
            tm.ParseDateTime("2007-11-24 8:00:00");

            tm.GetAsSystemTime(st);
            SetLocalTime(&st);*/
        }
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值