using System; using System.IO; using System.Linq; using System.Management; using Vinno.FIS.Sonopost.Features.Oled; using Vinno.FIS.Sonopost.Managers; using Vinno.FIS.Sonopost.Managers.Interfaces; using Vinno.IUS.Common.Log; namespace Vinno.FIS.Sonopost.Helpers { public class SystemHelper { private static IOledManager _oledManager; private const string BackupFileName = "Flyinsono_SonoPost_"; private const string BackupFolderPath = @"C:\SonopostBackup"; public static void Init() { _oledManager = AppManager.Instance.GetManager(); } /// /// 系统还原 /// public static void RestoreSystem() { try { ManagementPath path = new ManagementPath(@"\\.\root\default:SystemRestore"); ManagementClass mc = new ManagementClass(path); foreach (ManagementBaseObject item in mc.GetInstances()) { //CreationTime 创建时间 var creationTime = item.GetPropertyValue("CreationTime"); //Description 还原点名称(描述) var description = item.GetPropertyValue("Description").ToString(); //EventType 默认:100 var eventType = item.GetPropertyValue("EventType"); //RestorePointType 值定义:16(手动),18(关键更新),0(安装),1(应用卸载),10(安装驱动),12(控制面板添加删除) var restorePointType = item.GetPropertyValue("RestorePointType"); //SequenceNumber var sequenceNumber = item.GetPropertyValue("SequenceNumber"); Logger.WriteLineInfo($"CreateTime:{creationTime} Desctiption:{description} EventType:{eventType} RestorePointType:{restorePointType} SequenceNum:{sequenceNumber}"); if (description == "SonopostInstall") { Logger.WriteLineInfo("Start Restore!"); var result = mc.InvokeMethod("Restore", new object[] { sequenceNumber }); if (int.Parse(result.ToString()) == 0) { Logger.WriteLineInfo($"Restore State :{result}"); RebootSystem(); } else { Logger.WriteLineInfo($"Restore Error State :{result}"); _oledManager.ShowStatus(OledMessage.Error); } return; } } Logger.WriteLineInfo($"Restore Error:Didn't find the backUp file."); _oledManager.ShowStatus(OledMessage.Error); } catch (Exception e) { Logger.WriteLineError($"ResetSystem Error:{e}"); } } /// /// 系统还原 /// public static void ResetSystem() { try { if (!Directory.Exists(BackupFolderPath)) { Logger.WriteLineError($"ResetSystem Error:Backup Folder is Missing:{BackupFolderPath}"); _oledManager.ShowStatus(OledMessage.Error); return; } var backupFile = Directory.GetFiles(BackupFolderPath, BackupFileName + "*.zip", SearchOption.TopDirectoryOnly).FirstOrDefault(); if (backupFile == null) { Logger.WriteLineError($"ResetSystem Error:Backup File is Missing"); _oledManager.ShowStatus(OledMessage.Error); return; } else { Logger.WriteLineError($"Backup File Found:{backupFile}"); UpgradeHelper.StartUpgrade(backupFile, true, true); } } catch (Exception e) { Logger.WriteLineError($"ResetSystem Error:{e}"); _oledManager.ShowStatus(OledMessage.Error); } } /// /// 重启操作系统 /// public static void RebootSystem() { try { ConnectionOptions options = new ConnectionOptions(); options.EnablePrivileges = true; ManagementScope scope = new ManagementScope(@"\\.\root\cimv2", options); scope.Connect(); //Query system for Operating System information ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_OperatingSystem"); ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query); foreach (ManagementObject m in searcher.Get()) { // reboot system uint ret = (uint)m.InvokeMethod("Reboot", null); if (ret != 0) { Logger.WriteLineError("Reboot System Error!"); } } } catch (Exception e) { Logger.WriteLineError($"Reboot System Error:{e}"); } } /// /// get disk free space, unit GB /// /// /// public static long GetHardDiskFreeSpace(string path) { long totalSize = 0; var rootPath = Path.GetPathRoot(path); DriveInfo[] drives = DriveInfo.GetDrives(); foreach (DriveInfo drive in drives) { if (drive.Name == rootPath) { totalSize = drive.TotalFreeSpace / (1024 * 1024 * 1024); break; } } return totalSize; } } }