查看单个帖子
旧 2010-04-15, 12:29 AM   #2
huangyhg
超级版主
 
huangyhg的头像
 
注册日期: 04-03
帖子: 18592
精华: 36
现金: 249466 标准币
资产: 1080358888 标准币
huangyhg 向着好的方向发展
默认 回复: 使用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]
huangyhg离线中   回复时引用此帖