在WIN2000下如何用C#改变系统日期时间--急(在线等待)

yangzhiguo 2003-09-22 10:31:06
在WIN2000下如何用C#改变系统日期时间--谢谢
...全文
31 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ismezy2002 2003-09-22
  • 打赏
  • 举报
回复
using System.Runtime.InteropServices;

public struct SystemTime
{
public short Year;
public short Month;
public short DayOfWeek;
public short Day;
public short Hour;
public short Minute;
public short Second;
public short Milliseconds;
}

public MyDateTime
{
public MyDateTime()
{
}
[DllImport("Kernel32.dll", CharSet=CharSet.Auto)]
private static extern bool SetSystemTime(ref SystemTime st);

public bool SetSystemTime(int year,int mon,int day,int hour,int min,int sec)
{
SystemTime st = new SystemTime();
st.Year = (short)year;
st.Month = (short)mon;
st.Day = (short)day;
st.Hour = (short)hour;
st.Minute = (short)min;
st.Second = (short)sec;
return SetSystemTime(ref st);
}
}
Soking 2003-09-22
  • 打赏
  • 举报
回复
or
DataTime.Now = DataTime.Prase("---");
Soking 2003-09-22
  • 打赏
  • 举报
回复
DataTime.Now = new DataTime(---);
甴曱 2003-09-22
  • 打赏
  • 举报
回复
其中第一种方法比较简单,你可以参考:
http://expert.csdn.net/Expert/TopicView1.asp?id=1680542
甴曱 2003-09-22
  • 打赏
  • 举报
回复
1、调用控制面板选项:timedate.cpl
2、使用WMI进行改变:
using System;
using System.Runtime.InteropServices;

namespace ConsoleApplication20
{
[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 wMilliseconds;

public DateTime ToDateTime()
{
return new DateTime(wYear, wMonth, wDay, wHour, wMinute, wSecond,
wMilliseconds);
}
}

class Win32API
{
[DllImport("Kernel32.dll")]
public static extern bool SetSystemTime(ref SYSTEMTIME st);
[DllImport("Kernel32.dll")]
public static extern void GetSystemTime(out SYSTEMTIME st);
}

class Class1
{
[STAThread]
static void Main(string[] args)
{
SYSTEMTIME st;
Win32API.GetSystemTime(out st); // gets current time
Console.WriteLine( st.ToDateTime().ToLocalTime().ToString() );
st.wMinute++; // Adjust minutes
if (!Win32API.SetSystemTime(ref st)) // sets system time
Console.WriteLine("An error occured setting the system time");
Win32API.GetSystemTime(out st); // gets current time
Console.WriteLine( st.ToDateTime().ToLocalTime().ToString() );
Console.ReadLine();
}
}
}
_weiKun_ 2003-09-22
  • 打赏
  • 举报
回复
用C#的WMI编程,可以远程控制服务器
_weiKun_ 2003-09-22
  • 打赏
  • 举报
回复
用C#的WMI编程,可以远程控制服务器

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧