.net中设置系统时间

转自 http://search.csdn.net/Expert/topic/847/847765.xml?temp=.8445551

回复人: 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

希望能对您有所帮助!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值