123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- 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<IOledManager>();
- }
- /// <summary>
- /// 系统还原
- /// </summary>
- 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}");
- }
- }
- /// <summary>
- /// 系统还原
- /// </summary>
- 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);
- }
- }
- /// <summary>
- /// 重启操作系统
- /// </summary>
- 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}");
- }
- }
- /// <summary>
- /// get disk free space, unit GB
- /// </summary>
- /// <param name="path"></param>
- /// <returns></returns>
- 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;
- }
- }
- }
|