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

需要做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("修改系统时间失败!");
                    }
                }
            });
        }


    }
}

你可能感兴趣的:(c#,开发语言)