|
| 1 | +using GeneralUpdate.Drivelution; |
| 2 | +using GeneralUpdate.Drivelution.Abstractions.Configuration; |
| 3 | +using GeneralUpdate.Drivelution.Abstractions.Models; |
| 4 | + |
| 5 | +try |
| 6 | +{ |
| 7 | + Console.WriteLine($"=== GeneralUpdate.Drivelution 示例程序 ==="); |
| 8 | + Console.WriteLine($"=== GeneralUpdate.Drivelution Sample Program ===\n"); |
| 9 | + Console.WriteLine($"初始化时间:{DateTime.Now}"); |
| 10 | + Console.WriteLine($"Initialization Time: {DateTime.Now}\n"); |
| 11 | + |
| 12 | + // ======================================== |
| 13 | + // 1. 获取平台信息 / Get Platform Information |
| 14 | + // ======================================== |
| 15 | + Console.WriteLine("=== 1. 获取平台信息 / Get Platform Information ==="); |
| 16 | + var platformInfo = GeneralDrivelution.GetPlatformInfo(); |
| 17 | + Console.WriteLine($"平台 / Platform: {platformInfo.Platform}"); |
| 18 | + Console.WriteLine($"操作系统 / Operating System: {platformInfo.OperatingSystem}"); |
| 19 | + Console.WriteLine($"架构 / Architecture: {platformInfo.Architecture}"); |
| 20 | + Console.WriteLine($"系统版本 / System Version: {platformInfo.SystemVersion}"); |
| 21 | + Console.WriteLine($"是否支持 / Is Supported: {(platformInfo.IsSupported ? "是/Yes" : "否/No")}"); |
| 22 | + Console.WriteLine($"\n详细信息 / Full Info: {platformInfo}"); |
| 23 | + Console.WriteLine("\n" + new string('=', 80) + "\n"); |
| 24 | + |
| 25 | + // ======================================== |
| 26 | + // 2. 创建驱动更新实例 / Create Driver Updater Instance |
| 27 | + // ======================================== |
| 28 | + Console.WriteLine("=== 2. 创建驱动更新实例 / Create Driver Updater Instance ==="); |
| 29 | + |
| 30 | + // 使用默认配置创建 / Create with default configuration |
| 31 | + var updater = GeneralDrivelution.Create(); |
| 32 | + Console.WriteLine("✓ 默认实例创建成功 / Default instance created successfully"); |
| 33 | + |
| 34 | + // 使用自定义配置创建 / Create with custom configuration |
| 35 | + var options = new DrivelutionOptions |
| 36 | + { |
| 37 | + DefaultBackupPath = "./driver_backups", |
| 38 | + DefaultRetryCount = 5, |
| 39 | + DefaultRetryIntervalSeconds = 3, |
| 40 | + AutoCleanupBackups = true, |
| 41 | + BackupsToKeep = 5 |
| 42 | + }; |
| 43 | + var customUpdater = GeneralDrivelution.Create(options); |
| 44 | + Console.WriteLine("✓ 自定义实例创建成功 / Custom instance created successfully"); |
| 45 | + Console.WriteLine($" 备份路径 / Backup Path: {options.DefaultBackupPath}"); |
| 46 | + Console.WriteLine($" 重试次数 / Retry Count: {options.DefaultRetryCount}"); |
| 47 | + Console.WriteLine($" 自动清理备份 / Auto Cleanup Backups: {options.AutoCleanupBackups}"); |
| 48 | + Console.WriteLine("\n" + new string('=', 80) + "\n"); |
| 49 | + |
| 50 | + // ======================================== |
| 51 | + // 3. 从目录读取驱动信息 / Get Drivers from Directory |
| 52 | + // ======================================== |
| 53 | + Console.WriteLine("=== 3. 从目录读取驱动信息 / Get Drivers from Directory ==="); |
| 54 | + |
| 55 | + // 创建测试驱动目录 / Create test driver directory |
| 56 | + var testDriverDir = Path.Combine(Path.GetTempPath(), "test_drivers"); |
| 57 | + Directory.CreateDirectory(testDriverDir); |
| 58 | + Console.WriteLine($"测试目录 / Test Directory: {testDriverDir}"); |
| 59 | + |
| 60 | + // 根据平台创建测试驱动文件 / Create test driver files based on platform |
| 61 | + if (platformInfo.Platform == "Windows") |
| 62 | + { |
| 63 | + // 创建模拟的 Windows INF 文件 / Create mock Windows INF file |
| 64 | + var infFile = Path.Combine(testDriverDir, "test_driver.inf"); |
| 65 | + var infContent = @" |
| 66 | +[Version] |
| 67 | +Signature=""$Windows NT$"" |
| 68 | +Class=System |
| 69 | +ClassGuid={4d36e97d-e325-11ce-bfc1-08002be10318} |
| 70 | +Provider=%ManufacturerName% |
| 71 | +DriverVer=02/12/2024,1.2.3.0 |
| 72 | +CatalogFile=test_driver.cat |
| 73 | +
|
| 74 | +[Manufacturer] |
| 75 | +%ManufacturerName%=Standard,NTamd64 |
| 76 | +
|
| 77 | +[Standard.NTamd64] |
| 78 | +%DeviceDesc%=TestDriver_Install,PCI\VEN_8086&DEV_1234 |
| 79 | +
|
| 80 | +[TestDriver_Install] |
| 81 | +CopyFiles=Drivers_Dir |
| 82 | +
|
| 83 | +[Drivers_Dir] |
| 84 | +test_driver.sys |
| 85 | +
|
| 86 | +[DestinationDirs] |
| 87 | +Drivers_Dir=12 |
| 88 | +
|
| 89 | +[Strings] |
| 90 | +ManufacturerName=""Sample Driver Manufacturer"" |
| 91 | +DeviceDesc=""Sample Test Driver"" |
| 92 | +"; |
| 93 | + File.WriteAllText(infFile, infContent); |
| 94 | + Console.WriteLine($"✓ 已创建测试 INF 文件 / Created test INF file: {Path.GetFileName(infFile)}"); |
| 95 | + } |
| 96 | + else if (platformInfo.Platform == "Linux") |
| 97 | + { |
| 98 | + // 创建模拟的 Linux 内核模块文件 / Create mock Linux kernel module file |
| 99 | + var koFile = Path.Combine(testDriverDir, "test_driver.ko"); |
| 100 | + File.WriteAllText(koFile, "Mock Linux kernel module content"); |
| 101 | + Console.WriteLine($"✓ 已创建测试 KO 文件 / Created test KO file: {Path.GetFileName(koFile)}"); |
| 102 | + } |
| 103 | + |
| 104 | + // 获取目录中的驱动信息 / Get drivers from directory |
| 105 | + var drivers = await GeneralDrivelution.GetDriversFromDirectoryAsync(testDriverDir); |
| 106 | + Console.WriteLine($"\n发现驱动数量 / Drivers Found: {drivers.Count}"); |
| 107 | + |
| 108 | + if (drivers.Count > 0) |
| 109 | + { |
| 110 | + foreach (var driver in drivers) |
| 111 | + { |
| 112 | + Console.WriteLine($"\n驱动信息 / Driver Information:"); |
| 113 | + Console.WriteLine($" 名称 / Name: {driver.Name}"); |
| 114 | + Console.WriteLine($" 版本 / Version: {driver.Version}"); |
| 115 | + Console.WriteLine($" 文件路径 / File Path: {driver.FilePath}"); |
| 116 | + Console.WriteLine($" 目标操作系统 / Target OS: {driver.TargetOS}"); |
| 117 | + Console.WriteLine($" 架构 / Architecture: {driver.Architecture}"); |
| 118 | + Console.WriteLine($" 哈希值 / Hash: {driver.Hash}"); |
| 119 | + Console.WriteLine($" 哈希算法 / Hash Algorithm: {driver.HashAlgorithm}"); |
| 120 | + } |
| 121 | + } |
| 122 | + else |
| 123 | + { |
| 124 | + Console.WriteLine("未发现驱动文件 / No driver files found"); |
| 125 | + } |
| 126 | + Console.WriteLine("\n" + new string('=', 80) + "\n"); |
| 127 | + |
| 128 | + // ======================================== |
| 129 | + // 4. 验证驱动文件 / Validate Driver File |
| 130 | + // ======================================== |
| 131 | + Console.WriteLine("=== 4. 验证驱动文件 / Validate Driver File ==="); |
| 132 | + |
| 133 | + // 创建测试驱动文件 / Create test driver file |
| 134 | + var testDriverFile = Path.Combine(testDriverDir, "sample_driver.sys"); |
| 135 | + File.WriteAllText(testDriverFile, "Sample driver binary content"); |
| 136 | + |
| 137 | + var driverInfo = new DriverInfo |
| 138 | + { |
| 139 | + Name = "Sample Driver", |
| 140 | + Version = "1.0.0", |
| 141 | + FilePath = testDriverFile, |
| 142 | + TargetOS = platformInfo.OperatingSystem, |
| 143 | + Architecture = platformInfo.Architecture, |
| 144 | + Description = "示例驱动程序 / Sample driver for demonstration", |
| 145 | + ReleaseDate = DateTime.Now |
| 146 | + }; |
| 147 | + |
| 148 | + Console.WriteLine($"驱动名称 / Driver Name: {driverInfo.Name}"); |
| 149 | + Console.WriteLine($"驱动版本 / Driver Version: {driverInfo.Version}"); |
| 150 | + Console.WriteLine($"文件路径 / File Path: {driverInfo.FilePath}"); |
| 151 | + |
| 152 | + var isValid = await GeneralDrivelution.ValidateAsync(driverInfo); |
| 153 | + Console.WriteLine($"\n验证结果 / Validation Result: {(isValid ? "✓ 通过/Passed" : "✗ 失败/Failed")}"); |
| 154 | + Console.WriteLine("\n" + new string('=', 80) + "\n"); |
| 155 | + |
| 156 | + // ======================================== |
| 157 | + // 5. 快速更新驱动 / Quick Update Driver |
| 158 | + // ======================================== |
| 159 | + Console.WriteLine("=== 5. 快速更新驱动 / Quick Update Driver ==="); |
| 160 | + Console.WriteLine("注意:此示例仅演示API调用,不会实际安装驱动"); |
| 161 | + Console.WriteLine("Note: This example only demonstrates API usage, will not actually install drivers\n"); |
| 162 | + |
| 163 | + // 使用默认策略的快速更新 / Quick update with default strategy |
| 164 | + Console.WriteLine("使用默认策略 / Using default strategy:"); |
| 165 | + Console.WriteLine(" • 需要备份 / Requires Backup: 是/Yes"); |
| 166 | + Console.WriteLine(" • 重试次数 / Retry Count: 3"); |
| 167 | + Console.WriteLine(" • 重试间隔 / Retry Interval: 5秒/seconds"); |
| 168 | + |
| 169 | + var quickResult = await GeneralDrivelution.QuickUpdateAsync(driverInfo); |
| 170 | + |
| 171 | + Console.WriteLine($"\n更新结果 / Update Result:"); |
| 172 | + Console.WriteLine($" 状态 / Status: {quickResult.Status}"); |
| 173 | + Console.WriteLine($" 成功 / Success: {(quickResult.Success ? "是/Yes" : "否/No")}"); |
| 174 | + Console.WriteLine($" 开始时间 / Start Time: {quickResult.StartTime:yyyy-MM-dd HH:mm:ss}"); |
| 175 | + Console.WriteLine($" 结束时间 / End Time: {quickResult.EndTime:yyyy-MM-dd HH:mm:ss}"); |
| 176 | + Console.WriteLine($" 耗时 / Duration: {quickResult.DurationMs} ms"); |
| 177 | + Console.WriteLine($" 消息 / Message: {quickResult.Message}"); |
| 178 | + |
| 179 | + if (quickResult.Error != null) |
| 180 | + { |
| 181 | + Console.WriteLine($" 错误信息 / Error: {quickResult.Error.Message}"); |
| 182 | + } |
| 183 | + |
| 184 | + if (quickResult.StepLogs.Count > 0) |
| 185 | + { |
| 186 | + Console.WriteLine($"\n 执行步骤 / Execution Steps:"); |
| 187 | + foreach (var log in quickResult.StepLogs) |
| 188 | + { |
| 189 | + Console.WriteLine($" - {log}"); |
| 190 | + } |
| 191 | + } |
| 192 | + Console.WriteLine("\n" + new string('=', 80) + "\n"); |
| 193 | + |
| 194 | + // ======================================== |
| 195 | + // 6. 使用自定义策略更新 / Update with Custom Strategy |
| 196 | + // ======================================== |
| 197 | + Console.WriteLine("=== 6. 使用自定义策略更新 / Update with Custom Strategy ==="); |
| 198 | + |
| 199 | + var strategy = new UpdateStrategy |
| 200 | + { |
| 201 | + Mode = UpdateMode.Full, |
| 202 | + RequireBackup = true, |
| 203 | + BackupPath = Path.Combine(testDriverDir, "backups"), |
| 204 | + RetryCount = 5, |
| 205 | + RetryIntervalSeconds = 3, |
| 206 | + RestartMode = RestartMode.Prompt, |
| 207 | + TimeoutSeconds = 120, |
| 208 | + SkipHashValidation = false, |
| 209 | + SkipSignatureValidation = false |
| 210 | + }; |
| 211 | + |
| 212 | + Console.WriteLine("自定义策略配置 / Custom Strategy Configuration:"); |
| 213 | + Console.WriteLine($" 更新模式 / Update Mode: {strategy.Mode}"); |
| 214 | + Console.WriteLine($" 需要备份 / Require Backup: {(strategy.RequireBackup ? "是/Yes" : "否/No")}"); |
| 215 | + Console.WriteLine($" 备份路径 / Backup Path: {strategy.BackupPath}"); |
| 216 | + Console.WriteLine($" 重试次数 / Retry Count: {strategy.RetryCount}"); |
| 217 | + Console.WriteLine($" 重试间隔 / Retry Interval: {strategy.RetryIntervalSeconds} 秒/seconds"); |
| 218 | + Console.WriteLine($" 重启模式 / Restart Mode: {strategy.RestartMode}"); |
| 219 | + Console.WriteLine($" 超时时间 / Timeout: {strategy.TimeoutSeconds} 秒/seconds"); |
| 220 | + |
| 221 | + var customResult = await GeneralDrivelution.QuickUpdateAsync(driverInfo, strategy); |
| 222 | + |
| 223 | + Console.WriteLine($"\n更新结果 / Update Result:"); |
| 224 | + Console.WriteLine($" 状态 / Status: {customResult.Status}"); |
| 225 | + Console.WriteLine($" 成功 / Success: {(customResult.Success ? "是/Yes" : "否/No")}"); |
| 226 | + Console.WriteLine($" 备份路径 / Backup Path: {customResult.BackupPath ?? "无/None"}"); |
| 227 | + Console.WriteLine($" 已回滚 / Rolled Back: {(customResult.RolledBack ? "是/Yes" : "否/No")}"); |
| 228 | + Console.WriteLine($" 耗时 / Duration: {customResult.DurationMs} ms"); |
| 229 | + Console.WriteLine("\n" + new string('=', 80) + "\n"); |
| 230 | + |
| 231 | + // ======================================== |
| 232 | + // 7. 备份和回滚操作 / Backup and Rollback Operations |
| 233 | + // ======================================== |
| 234 | + Console.WriteLine("=== 7. 备份和回滚操作 / Backup and Rollback Operations ==="); |
| 235 | + |
| 236 | + // 备份驱动 / Backup driver |
| 237 | + var backupPath = Path.Combine(testDriverDir, "manual_backup"); |
| 238 | + Directory.CreateDirectory(backupPath); |
| 239 | + |
| 240 | + Console.WriteLine($"备份驱动到 / Backing up driver to: {backupPath}"); |
| 241 | + var backupSuccess = await updater.BackupAsync(driverInfo, backupPath); |
| 242 | + Console.WriteLine($"备份结果 / Backup Result: {(backupSuccess ? "✓ 成功/Success" : "✗ 失败/Failed")}"); |
| 243 | + |
| 244 | + if (backupSuccess) |
| 245 | + { |
| 246 | + // 模拟回滚操作 / Simulate rollback operation |
| 247 | + Console.WriteLine($"\n从备份回滚驱动 / Rolling back driver from backup"); |
| 248 | + var rollbackSuccess = await updater.RollbackAsync(backupPath); |
| 249 | + Console.WriteLine($"回滚结果 / Rollback Result: {(rollbackSuccess ? "✓ 成功/Success" : "✗ 失败/Failed")}"); |
| 250 | + } |
| 251 | + |
| 252 | + Console.WriteLine("\n" + new string('=', 80) + "\n"); |
| 253 | + |
| 254 | + // ======================================== |
| 255 | + // 清理测试文件 / Cleanup Test Files |
| 256 | + // ======================================== |
| 257 | + Console.WriteLine("=== 清理 / Cleanup ==="); |
| 258 | + if (Directory.Exists(testDriverDir)) |
| 259 | + { |
| 260 | + try |
| 261 | + { |
| 262 | + Directory.Delete(testDriverDir, true); |
| 263 | + Console.WriteLine("✓ 测试文件已清理 / Test files cleaned up"); |
| 264 | + } |
| 265 | + catch (Exception ex) |
| 266 | + { |
| 267 | + Console.WriteLine($"✗ 清理失败 / Cleanup failed: {ex.Message}"); |
| 268 | + } |
| 269 | + } |
| 270 | + |
| 271 | + Console.WriteLine("\n=== 示例程序执行完成 / Sample Program Completed ==="); |
| 272 | + Console.WriteLine($"完成时间:{DateTime.Now}"); |
| 273 | + Console.WriteLine($"Completion Time: {DateTime.Now}"); |
| 274 | +} |
| 275 | +catch (Exception e) |
| 276 | +{ |
| 277 | + Console.WriteLine($"\n异常 / Exception: {e.Message}"); |
| 278 | + Console.WriteLine($"堆栈跟踪 / Stack Trace:\n{e.StackTrace}"); |
| 279 | +} |
0 commit comments