首页 > 编程 > .NET > 正文

.net中设置系统时间

2024-07-10 13:05:01
字体:
来源:转载
供稿:网友
回复人: 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

希望能对您有所帮助!



发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表