需要做c#.net 项目的,有时间并且想赚零花钱的老哥,请加Q群:741058172。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
需要做c#.net 项目的,有时间并且想赚零花钱的老哥,请加Q群:741058172。
namespace 时间修改
{
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);
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("修改系统时间失败!");
}
}
});
}
}
}