WayneSystemTimeCE.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace Wayne.Lib
  4. {
  5. public class WayneSystemTimeCE : ISystemTime
  6. {
  7. public DateTime Now
  8. {
  9. get { return DateTime.Now; }
  10. }
  11. [DllImport("coredll.dll")]
  12. static extern bool SetSystemTime(
  13. ref SystemTime systemTime);
  14. [DllImport("coredll.dll")]
  15. static extern bool SetLocalTime(
  16. ref SystemTime systemTime);
  17. public void SetSystemTime(DateTime newTime)
  18. {
  19. SystemTime st = WayneSystemTime.ConvertToSystemTime(newTime.ToUniversalTime());
  20. bool rc = SetSystemTime(ref st);
  21. if (!rc)
  22. {
  23. int wrc = Marshal.GetLastWin32Error();
  24. throw new Exception("Error " + wrc + " setting system time");
  25. }
  26. }
  27. public void SetLocalTime(DateTime newTime)
  28. {
  29. // Microsoft.VisualBasic.DateAndTime.TimeOfDay = DateTime.SpecifyKind(newTime, DateTimeKind.Local);
  30. SystemTime st = WayneSystemTime.ConvertToSystemTime(newTime);
  31. bool rc = SetLocalTime(ref st);
  32. if (!rc)
  33. {
  34. int wrc = Marshal.GetLastWin32Error();
  35. throw new Exception("Error " + wrc + " setting local time");
  36. }
  37. }
  38. }
  39. }