From f722196a23cf8e7997d63cecbc42ef0b4c165750 Mon Sep 17 00:00:00 2001 From: Juster Zhu Date: Sat, 13 Jan 2024 22:57:28 +0800 Subject: [PATCH] Update MySample.cs --- src/c#/GeneralUpdate.Client/MySample.cs | 68 +++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/src/c#/GeneralUpdate.Client/MySample.cs b/src/c#/GeneralUpdate.Client/MySample.cs index c84093f..6bc123e 100644 --- a/src/c#/GeneralUpdate.Client/MySample.cs +++ b/src/c#/GeneralUpdate.Client/MySample.cs @@ -12,6 +12,9 @@ using GeneralUpdate.Core.Domain.Enum; using GeneralUpdate.Core.Events.CommonArgs; using GeneralUpdate.Differential; using System.IO; +using GeneralUpdate.Core.Driver; +using Microsoft.VisualBasic; +using System.Diagnostics; namespace GeneralUpdate.Client { @@ -231,5 +234,70 @@ namespace GeneralUpdate.Client } #endregion + + #region 测试驱动功能 + + public void TestDrive() + { + var path1 = "D:\\packet\\source"; + var path2 = "D:\\packet\\target"; + + var drivers = GetAllDriverDirectories(path1); + + var information = new DriverInformation.Builder() + .SetInstallDirectory(path1) + .SetOutPutDirectory(path2) + .SetDriverNames(drivers) + .Build(); + + var processor = new DriverProcessor(); + processor.AddCommand(new BackupDriverCommand(information)); + processor.AddCommand(new DeleteDriverCommand(information)); + processor.AddCommand(new InstallDriverCommand(information)); + processor.ProcessCommands(); + } + + /// + /// Identifies all folders containing driver files in the specified directory and returns the directory collection. + /// + /// + /// + private List GetAllDriverDirectories(string path) + { + var driverDirectories = new HashSet(); + try + { + foreach (string filePath in Directory.GetFiles(path)) + { + if (IsDriverFile(filePath)) + driverDirectories.Add(filePath); + } + + foreach (string directory in Directory.GetDirectories(path)) + { + driverDirectories.UnionWith(GetAllDriverDirectories(directory)); + } + } + catch (UnauthorizedAccessException) + { + Trace.WriteLine("No access directory:" + path); + } + catch (PathTooLongException) + { + Trace.WriteLine("Path overlength:" + path); + } + + return new List(driverDirectories); + } + + /// + /// Match the driver installation boot file. + /// + /// + /// + private bool IsDriverFile(string filePath) => + string.Equals(Path.GetExtension(filePath), ".inf", StringComparison.OrdinalIgnoreCase); + + #endregion } } -- Gitee