using System;
using ;
class Program
{
// 导入SetSystemTime函数
[
] [
] static extern bool SetSystemTime(ref SYSTEMTIME time);
// SYSTEMTIME结构体
[
] 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 SYSTEMTIME(int year, int month, int day, int hour, int minute, int second)
{
wYear = (ushort)year;
wMonth = (ushort)month;
wDay = (ushort)day;
wHour = (ushort)hour;
wMinute = (ushort)minute;
wSecond = (ushort)second;
wDayOfWeek = 0; // 通常由系统计算
wMilliseconds = 0; // 如果需要,可以设置
}
}
static void Main(string[] args)
{
// 设置新的系统时间
SYSTEMTIME newTime = new SYSTEMTIME(2023, 10, 1, 14, 30, 0);
if (!SetSystemTime(ref newTime))
{
// 处理错误
int errorCode = Marshal.GetLastWin32Error();
($"Failed to set system time. Error code: {errorCode}");
}
else
{
("System time set successfully.");
}
}
}