超级版主
注册日期: 04-03
帖子: 18592
精华: 36
现金: 249466 标准币
资产: 1080358888 标准币
|
回复: 使用c#设置系统时间(不同于以往的文章,是经过测试的)
NET并没有提供可以修改系统时间的名字空间(namespace),您可以通过Win32 API的SetSystemTime API函数来设置系统时间:
using System;
using System.Runtime.InteropServices;
[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 wMiliseconds;
}
public class Win32
{
[DllImport("Kernel32.dll")]
public static extern bool SetSystemTime( ref SystemTime sysTime );
[DllImport("Kernel32.dll")]
public static extern void GetSystemTime(ref SystemTime sysTime);
}
public class TestPInvoke
{
public static void Main()
{
SystemTime sysTime = new SystemTime();
sysTime.wYear = 2002;
sysTime.wMonth=1;
sysTime.wDay=1;
sysTime.wDayOfWeek=1;
sysTime.wHour=1;
sysTime.wMinute=1;
sysTime.wSecond=1;
sysTime.wMiliseconds=1;
Win32.SetSystemTime(ref sysTime);
}
}
__________________
借用达朗贝尔的名言:前进吧,你会得到信心!
[url="http://www.dimcax.com"]几何尺寸与公差标准[/url]
|