9f18abd15c887a618e5a0ab6aa855a8f2e90f0b5.svn-base 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Runtime.InteropServices;
  5. using Core.Mes.ClientFrameWork;
  6. namespace Core.Mes.ClientManager
  7. {
  8. class SetTime
  9. {
  10. //imports SetLocalTime function from kernel32.dll
  11. [DllImport("kernel32.dll", SetLastError = true)]
  12. public static extern int SetLocalTime(ref SystemTime lpSystemTime);
  13. public static void SetClientTime()
  14. {
  15. string strOut = "";
  16. object obj1 = ClientCommon._RemotingHelp.ExecuteMethod("ServerCommon", "Core.Mes.ServerCommon.UserInfoManager", "getDBSrvTime",
  17. null, out strOut);
  18. if (strOut != "") throw new Exception(strOut);
  19. if (obj1 != null)
  20. {
  21. // And then set up a structure with the required properties and call the api from code:
  22. DateTime dt = Convert.ToDateTime(obj1);
  23. SystemTime systNew = new SystemTime();
  24. // 设置属性
  25. systNew.wDay = Convert.ToInt16(dt.Day);
  26. systNew.wMonth = Convert.ToInt16(dt.Month);
  27. systNew.wYear = Convert.ToInt16(dt.Year);
  28. systNew.wHour = Convert.ToInt16(dt.Hour);
  29. systNew.wMinute = Convert.ToInt16(dt.Minute);
  30. systNew.wSecond = Convert.ToInt16(dt.Second);
  31. // 调用API,更新系统时间
  32. SetLocalTime(ref systNew);
  33. }
  34. }
  35. }
  36. //struct for date/time apis
  37. public struct SystemTime
  38. {
  39. public short wYear;
  40. public short wMonth;
  41. public short wDayOfWeek;
  42. public short wDay;
  43. public short wHour;
  44. public short wMinute;
  45. public short wSecond;
  46. public short wMilliseconds;
  47. }
  48. }