Skip to content

Commit 5d5c051

Browse files
CopilotJusterZhu
andauthored
Add console samples for Drivelution and Extension components (#32)
* Initial plan * Create Drivelution and Extension sample programs Co-authored-by: JusterZhu <11714536+JusterZhu@users.noreply.github.com> * Update .gitignore and replace .slnx with .sln files Co-authored-by: JusterZhu <11714536+JusterZhu@users.noreply.github.com> * Replace project references with NuGet package references version 10.2.1 Co-authored-by: JusterZhu <11714536+JusterZhu@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: JusterZhu <11714536+JusterZhu@users.noreply.github.com>
1 parent ce9094a commit 5d5c051

7 files changed

Lines changed: 793 additions & 1 deletion

File tree

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,8 @@ FakesAssemblies/
194194
*.v6.1
195195

196196
.idea/
197-
.vs/
197+
.vs/
198+
199+
# Generated extensions directory
200+
**/extensions/
201+
*.slnx
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net10.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="GeneralUpdate.Drivelution" Version="10.2.1" />
12+
</ItemGroup>
13+
14+
</Project>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.0.31903.59
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DrivelutionSample", "DrivelutionSample.csproj", "{8015E777-C917-47A3-A335-56EBFB7063F1}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Debug|x64 = Debug|x64
12+
Debug|x86 = Debug|x86
13+
Release|Any CPU = Release|Any CPU
14+
Release|x64 = Release|x64
15+
Release|x86 = Release|x86
16+
EndGlobalSection
17+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
18+
{8015E777-C917-47A3-A335-56EBFB7063F1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
19+
{8015E777-C917-47A3-A335-56EBFB7063F1}.Debug|Any CPU.Build.0 = Debug|Any CPU
20+
{8015E777-C917-47A3-A335-56EBFB7063F1}.Debug|x64.ActiveCfg = Debug|Any CPU
21+
{8015E777-C917-47A3-A335-56EBFB7063F1}.Debug|x64.Build.0 = Debug|Any CPU
22+
{8015E777-C917-47A3-A335-56EBFB7063F1}.Debug|x86.ActiveCfg = Debug|Any CPU
23+
{8015E777-C917-47A3-A335-56EBFB7063F1}.Debug|x86.Build.0 = Debug|Any CPU
24+
{8015E777-C917-47A3-A335-56EBFB7063F1}.Release|Any CPU.ActiveCfg = Release|Any CPU
25+
{8015E777-C917-47A3-A335-56EBFB7063F1}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{8015E777-C917-47A3-A335-56EBFB7063F1}.Release|x64.ActiveCfg = Release|Any CPU
27+
{8015E777-C917-47A3-A335-56EBFB7063F1}.Release|x64.Build.0 = Release|Any CPU
28+
{8015E777-C917-47A3-A335-56EBFB7063F1}.Release|x86.ActiveCfg = Release|Any CPU
29+
{8015E777-C917-47A3-A335-56EBFB7063F1}.Release|x86.Build.0 = Release|Any CPU
30+
EndGlobalSection
31+
GlobalSection(SolutionProperties) = preSolution
32+
HideSolutionNode = FALSE
33+
EndGlobalSection
34+
EndGlobal
Lines changed: 279 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,279 @@
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+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="GeneralUpdate.Extension" Version="10.2.1" />
12+
</ItemGroup>
13+
14+
</Project>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.0.31903.59
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExtensionSample", "ExtensionSample.csproj", "{5E892380-DB04-4680-8EB2-101A351EE9DB}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Debug|x64 = Debug|x64
12+
Debug|x86 = Debug|x86
13+
Release|Any CPU = Release|Any CPU
14+
Release|x64 = Release|x64
15+
Release|x86 = Release|x86
16+
EndGlobalSection
17+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
18+
{5E892380-DB04-4680-8EB2-101A351EE9DB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
19+
{5E892380-DB04-4680-8EB2-101A351EE9DB}.Debug|Any CPU.Build.0 = Debug|Any CPU
20+
{5E892380-DB04-4680-8EB2-101A351EE9DB}.Debug|x64.ActiveCfg = Debug|Any CPU
21+
{5E892380-DB04-4680-8EB2-101A351EE9DB}.Debug|x64.Build.0 = Debug|Any CPU
22+
{5E892380-DB04-4680-8EB2-101A351EE9DB}.Debug|x86.ActiveCfg = Debug|Any CPU
23+
{5E892380-DB04-4680-8EB2-101A351EE9DB}.Debug|x86.Build.0 = Debug|Any CPU
24+
{5E892380-DB04-4680-8EB2-101A351EE9DB}.Release|Any CPU.ActiveCfg = Release|Any CPU
25+
{5E892380-DB04-4680-8EB2-101A351EE9DB}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{5E892380-DB04-4680-8EB2-101A351EE9DB}.Release|x64.ActiveCfg = Release|Any CPU
27+
{5E892380-DB04-4680-8EB2-101A351EE9DB}.Release|x64.Build.0 = Release|Any CPU
28+
{5E892380-DB04-4680-8EB2-101A351EE9DB}.Release|x86.ActiveCfg = Release|Any CPU
29+
{5E892380-DB04-4680-8EB2-101A351EE9DB}.Release|x86.Build.0 = Release|Any CPU
30+
EndGlobalSection
31+
GlobalSection(SolutionProperties) = preSolution
32+
HideSolutionNode = FALSE
33+
EndGlobalSection
34+
EndGlobal

0 commit comments

Comments
 (0)