C# 时间修改器,修改系统本地时间

2023-12-30 17:33:02
需要做c#.net 项目的,有时间并且想赚零花钱的老哥,请加Q群:741058172using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
需要做c#.net 项目的,有时间并且想赚零花钱的老哥,请加Q群:741058172namespace 时间修改
{
    internal class Program
    {

        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern bool SetLocalTime(ref SystemTime st);

        [StructLayout(LayoutKind.Sequential)]
        public struct SystemTime
        {
            public ushort Year;
            public ushort Month;
            public ushort DayOfWeek;
            public ushort Day;
            public ushort Hour;
            public ushort Minute;
            public ushort Second;
            public ushort Milliseconds;
        }


        static void Main(string[] args)
        {

            Task.Run(() => {

                while (true)
                {
                    // 获取当前时间
                    DateTime now = DateTime.Now;

                    // 设置新的时间
                    DateTime newTime = new DateTime(2023, 5, 20, 8, 0, 0); // 设置为2023年12月31日早上8点

                    // 修改系统时间
                    SystemTime systemTime = new SystemTime();
                    systemTime.Year = (ushort)newTime.Year;
                    systemTime.Month = (ushort)newTime.Month;
                    systemTime.Day = (ushort)newTime.Day;
                    systemTime.Hour = (ushort)newTime.Hour;
                    systemTime.Minute = (ushort)newTime.Minute;
                    systemTime.Second = (ushort)newTime.Second;

                    if (SetLocalTime(ref systemTime))
                    {
                        Console.WriteLine("系统时间已修改为:" + newTime.ToString());
                    }
                    else
                    {
                        Console.WriteLine("修改系统时间失败!");
                    }
                }
            });
        }


    }
}

文章来源:https://blog.csdn.net/csdn2990/article/details/135306855
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。