SystemHelper.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Management;
  5. using Vinno.FIS.Sonopost.Features.Oled;
  6. using Vinno.FIS.Sonopost.Managers;
  7. using Vinno.FIS.Sonopost.Managers.Interfaces;
  8. using Vinno.IUS.Common.Log;
  9. namespace Vinno.FIS.Sonopost.Helpers
  10. {
  11. public class SystemHelper
  12. {
  13. private static IOledManager _oledManager;
  14. private const string BackupFileName = "Flyinsono_SonoPost_";
  15. private const string BackupFolderPath = @"C:\SonopostBackup";
  16. public static void Init()
  17. {
  18. _oledManager = AppManager.Instance.GetManager<IOledManager>();
  19. }
  20. /// <summary>
  21. /// 系统还原
  22. /// </summary>
  23. public static void RestoreSystem()
  24. {
  25. try
  26. {
  27. ManagementPath path = new ManagementPath(@"\\.\root\default:SystemRestore");
  28. ManagementClass mc = new ManagementClass(path);
  29. foreach (ManagementBaseObject item in mc.GetInstances())
  30. {
  31. //CreationTime 创建时间
  32. var creationTime = item.GetPropertyValue("CreationTime");
  33. //Description 还原点名称(描述)
  34. var description = item.GetPropertyValue("Description").ToString();
  35. //EventType 默认:100
  36. var eventType = item.GetPropertyValue("EventType");
  37. //RestorePointType 值定义:16(手动),18(关键更新),0(安装),1(应用卸载),10(安装驱动),12(控制面板添加删除)
  38. var restorePointType = item.GetPropertyValue("RestorePointType");
  39. //SequenceNumber
  40. var sequenceNumber = item.GetPropertyValue("SequenceNumber");
  41. Logger.WriteLineInfo($"CreateTime:{creationTime} Desctiption:{description} EventType:{eventType} RestorePointType:{restorePointType} SequenceNum:{sequenceNumber}");
  42. if (description == "SonopostInstall")
  43. {
  44. Logger.WriteLineInfo("Start Restore!");
  45. var result = mc.InvokeMethod("Restore", new object[] { sequenceNumber });
  46. if (int.Parse(result.ToString()) == 0)
  47. {
  48. Logger.WriteLineInfo($"Restore State :{result}");
  49. RebootSystem();
  50. }
  51. else
  52. {
  53. Logger.WriteLineInfo($"Restore Error State :{result}");
  54. _oledManager.ShowStatus(OledMessage.Error);
  55. }
  56. return;
  57. }
  58. }
  59. Logger.WriteLineInfo($"Restore Error:Didn't find the backUp file.");
  60. _oledManager.ShowStatus(OledMessage.Error);
  61. }
  62. catch (Exception e)
  63. {
  64. Logger.WriteLineError($"ResetSystem Error:{e}");
  65. }
  66. }
  67. /// <summary>
  68. /// 系统还原
  69. /// </summary>
  70. public static void ResetSystem()
  71. {
  72. try
  73. {
  74. if (!Directory.Exists(BackupFolderPath))
  75. {
  76. Logger.WriteLineError($"ResetSystem Error:Backup Folder is Missing:{BackupFolderPath}");
  77. _oledManager.ShowStatus(OledMessage.Error);
  78. return;
  79. }
  80. var backupFile = Directory.GetFiles(BackupFolderPath, BackupFileName + "*.zip", SearchOption.TopDirectoryOnly).FirstOrDefault();
  81. if (backupFile == null)
  82. {
  83. Logger.WriteLineError($"ResetSystem Error:Backup File is Missing");
  84. _oledManager.ShowStatus(OledMessage.Error);
  85. return;
  86. }
  87. else
  88. {
  89. Logger.WriteLineError($"Backup File Found:{backupFile}");
  90. UpgradeHelper.StartUpgrade(backupFile, true, true);
  91. }
  92. }
  93. catch (Exception e)
  94. {
  95. Logger.WriteLineError($"ResetSystem Error:{e}");
  96. _oledManager.ShowStatus(OledMessage.Error);
  97. }
  98. }
  99. /// <summary>
  100. /// 重启操作系统
  101. /// </summary>
  102. public static void RebootSystem()
  103. {
  104. try
  105. {
  106. ConnectionOptions options = new ConnectionOptions();
  107. options.EnablePrivileges = true;
  108. ManagementScope scope = new ManagementScope(@"\\.\root\cimv2", options);
  109. scope.Connect();
  110. //Query system for Operating System information
  111. ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
  112. ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
  113. foreach (ManagementObject m in searcher.Get())
  114. {
  115. // reboot system
  116. uint ret = (uint)m.InvokeMethod("Reboot", null);
  117. if (ret != 0)
  118. {
  119. Logger.WriteLineError("Reboot System Error!");
  120. }
  121. }
  122. }
  123. catch (Exception e)
  124. {
  125. Logger.WriteLineError($"Reboot System Error:{e}");
  126. }
  127. }
  128. /// <summary>
  129. /// get disk free space, unit GB
  130. /// </summary>
  131. /// <param name="path"></param>
  132. /// <returns></returns>
  133. public static long GetHardDiskFreeSpace(string path)
  134. {
  135. long totalSize = 0;
  136. var rootPath = Path.GetPathRoot(path);
  137. DriveInfo[] drives = DriveInfo.GetDrives();
  138. foreach (DriveInfo drive in drives)
  139. {
  140. if (drive.Name == rootPath)
  141. {
  142. totalSize = drive.TotalFreeSpace / (1024 * 1024 * 1024);
  143. break;
  144. }
  145. }
  146. return totalSize;
  147. }
  148. }
  149. }