12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- using System;
- using System.Runtime.InteropServices;
- namespace Wayne.Lib
- {
-
-
-
- [StructLayout(LayoutKind.Sequential)]
- public struct SystemTime
- {
- [MarshalAs(UnmanagedType.U2)]
- public short Year;
- [MarshalAs(UnmanagedType.U2)]
- public short Month;
- [MarshalAs(UnmanagedType.U2)]
- public short DayOfWeek;
- [MarshalAs(UnmanagedType.U2)]
- public short Day;
- [MarshalAs(UnmanagedType.U2)]
- public short Hour;
- [MarshalAs(UnmanagedType.U2)]
- public short Minute;
- [MarshalAs(UnmanagedType.U2)]
- public short Second;
- [MarshalAs(UnmanagedType.U2)]
- public short Milliseconds;
- }
-
-
-
-
-
-
- public class WayneSystemTime : ISystemTime
- {
-
-
-
- public DateTime Now
- {
- get { return DateTime.Now; }
- set { SetSystemTime(value); }
- }
- [DllImport("kernel32.dll")]
- public static extern bool SetSystemTime(ref SystemTime systemTime);
- [DllImport("kernel32.dll")]
- public static extern bool SetLocalTime(ref SystemTime systemTime);
-
-
-
-
-
- public void SetSystemTime(DateTime newTime)
- {
- SystemTime st = ConvertToSystemTime(newTime);
- bool rc = SetSystemTime(ref st);
- if (!rc)
- {
- int wrc = Marshal.GetLastWin32Error();
- throw new Exception("Error " + wrc + " setting system time");
- }
- }
-
-
-
-
- public void SetLocalTime(DateTime newTime)
- {
-
- SystemTime st = ConvertToSystemTime(newTime);
- bool rc = SetLocalTime(ref st);
- if (!rc)
- {
- int wrc = Marshal.GetLastWin32Error();
- throw new Exception("Error " + wrc + " setting local time");
- }
- }
- private static SystemTime ConvertToSystemTime(DateTime t)
- {
- SystemTime st = new SystemTime();
- st.Year = (short) t.Year;
- st.Month = (short) t.Month;
- st.Day = (short) t.Day;
- st.Hour = (short) t.Hour;
- st.Minute = (short) t.Minute;
- st.Second = (short) t.Second;
- return st;
- }
- }
- }
|