张晓华的博客

        2005年,最后一场雪,还是下了,虽然凄凉了一些!
posts - 185, comments - 100, trackbacks - 8, articles - 1

Saturday, October 11, 2008

/// <summary>
 ///系统时间类
 /// </summary>
 [ StructLayout( LayoutKind.Sequential )]
 public class SystemTime
 {
  public ushort year;
  public ushort month;
  public ushort dayofweek;
  public ushort day;
  public ushort hour;
  public ushort minute;
  public ushort second;
  public ushort milliseconds;
 }

 /// <summary>
 /// Windows API方法
 /// </summary>
 public class WinAPI
 {
  [ DllImport( "Kernel32.dll" )]
  private static extern Boolean SetSystemTime([In,Out] SystemTime st);

  /// <summary>
  /// 设置系统时间
  /// </summary>
  /// <param name="newdatetime">新时间</param>
  /// <returns></returns>
  public static bool SetSysTime(DateTime newdatetime)
  {
   SystemTime st = new SystemTime();
   st.year    = Convert.ToUInt16(newdatetime.Year);
   st.month   = Convert.ToUInt16(newdatetime.Month);
   st.day    = Convert.ToUInt16(newdatetime.Day);
   st.dayofweek  = Convert.ToUInt16(newdatetime.DayOfWeek);
   st.hour    = Convert.ToUInt16(newdatetime.Hour - TimeZone.CurrentTimeZone.GetUtcOffset(new DateTime(2001,09,01)).Hours);
   st.minute   = Convert.ToUInt16(newdatetime.Minute);
   st.second   = Convert.ToUInt16(newdatetime.Second);
   st.milliseconds  = Convert.ToUInt16(newdatetime.Millisecond);
   return SetSystemTime(st);
  }

 }

posted @ 2:37 AM | Feedback (0)