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

主  题:  在WIN2000下如何用C#改变系统日期时间--急(在线等待)
作  者:  yangzhiguo (小楊)  
等  级:  ^
信 誉 值:  100
所属论坛:  .NET技术 C#
问题点数:  100
回复次数:  6
发表时间:  2003-9-22 10:31:07

在WIN2000下如何用C#改变系统日期时间--谢谢



回复人: weiKun(Virus) ( 五级(中级)) 信誉:100 2003-9-22 10:35:33 得分:0

用C#的WMI编程,可以远程控制服务器

回复人: weiKun(Virus) ( 五级(中级)) 信誉:100 2003-9-22 10:36:18 得分:0

用C#的WMI编程,可以远程控制服务器

回复人: cocosoft(pengyun) ( 两星(中级)) 信誉:116 2003-9-22 10:40:36 得分:100

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();
    }
  }
}

回复人: cocosoft(pengyun) ( 两星(中级)) 信誉:116 2003-9-22 10:41:53 得分:0

其中第一种方法比较简单,你可以参考:
http://expert.csdn.net/Expert/TopicView1.asp?id=1680542

回复人: Soking(Soking) ( 四级(中级)) 信誉:100 2003-9-22 10:51:36 得分:0

DataTime.Now = new DataTime(---);

回复人: Soking(Soking) ( 四级(中级)) 信誉:100 2003-9-22 10:54:23 得分:0

or
DataTime.Now = DataTime.Prase("---");

回复人: ismezy2002(扬) ( 三级(初级)) 信誉:95 2003-9-22 11:12:15 得分:0

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);
    }
}

该问题已经结贴 ,得分记录: cocosoft (100)、

Contributors: FHL