.net中设置系统时间

80酷酷网    80kuku.com

  回复人: acptvb(微软全球技术中心 VB技术支持) ( )

感谢您使用微软产品。

.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 =3D 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);
}
}

请参考microsoft.public.dotnet.framework新闻组上的帖子:
http://groups.google.com/groups?hl=zh-CN&lr=lang_zh-CN|lang_zh-TW|lang_en&ie=UTF-8&selm=O10nq1XjAHA.2184%40tkmsftngp02

希望能对您有所帮助!



分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: