Skip to content

Commit ebb2c54

Browse files
committed
add driver sample
1 parent 1f5f59f commit ebb2c54

File tree

7 files changed

+192
-0
lines changed

7 files changed

+192
-0
lines changed

src/Driver/DriverSample/.idea/.idea.DriverSample/.idea/.gitignore

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Driver/DriverSample/.idea/.idea.DriverSample/.idea/encodings.xml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Driver/DriverSample/.idea/.idea.DriverSample/.idea/indexLayout.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Driver/DriverSample/.idea/.idea.DriverSample/.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
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.Core" Version="9.0.0" />
12+
</ItemGroup>
13+
14+
</Project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DriverSample", "DriverSample.csproj", "{B8406200-B726-47A5-A1CD-BCF1C643DCB5}"
4+
EndProject
5+
Global
6+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7+
Debug|Any CPU = Debug|Any CPU
8+
Release|Any CPU = Release|Any CPU
9+
EndGlobalSection
10+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
11+
{B8406200-B726-47A5-A1CD-BCF1C643DCB5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
12+
{B8406200-B726-47A5-A1CD-BCF1C643DCB5}.Debug|Any CPU.Build.0 = Debug|Any CPU
13+
{B8406200-B726-47A5-A1CD-BCF1C643DCB5}.Release|Any CPU.ActiveCfg = Release|Any CPU
14+
{B8406200-B726-47A5-A1CD-BCF1C643DCB5}.Release|Any CPU.Build.0 = Release|Any CPU
15+
EndGlobalSection
16+
EndGlobal

src/Driver/DriverSample/Program.cs

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
using System.Text;
2+
using GeneralUpdate.Common.Download;
3+
using GeneralUpdate.Common.FileBasic;
4+
using GeneralUpdate.Common.Internal;
5+
using GeneralUpdate.Common.Internal.Bootstrap;
6+
using GeneralUpdate.Common.Shared.Object;
7+
using GeneralUpdate.Core;
8+
using GeneralUpdate.Core.Driver;
9+
10+
namespace GeneralUpdate.Upgrad
11+
{
12+
internal class Program
13+
{
14+
static async Task Main(string[] args)
15+
{
16+
try
17+
{
18+
/*
19+
Special Note:
20+
Since driver updates will trigger the need for elevated permissions (UAC),
21+
it is temporarily necessary to lower the operating system's security level before running the code and restore it after the update is complete.
22+
Otherwise, the following implementation will not function properly.
23+
24+
ref: https://www.justerzhu.cn/docs/guide/Permission
25+
*/
26+
27+
//A driver package field mapping table for Chinese operating systems, used to parse the strings containing information about all driver packages.
28+
var fieldMappingsCN = new Dictionary<string, string>
29+
{
30+
{ "PublishedName", "发布名称" },
31+
{ "OriginalName", "原始名称" },
32+
{ "Provider", "提供程序名称" },
33+
{ "ClassName", "类名" },
34+
{ "ClassGUID", "类 GUID" },
35+
{ "Version", "驱动程序版本" },
36+
{ "Signer", "签名者姓名" }
37+
};
38+
39+
//A driver package field mapping table for English operating systems, used to parse the strings containing information about all driver packages.
40+
var fieldMappingsEN = new Dictionary<string, string>
41+
{
42+
{ "PublishedName", "Driver" },
43+
{ "OriginalName", "OriginalFileName" },
44+
{ "Provider", "ProviderName" },
45+
{ "ClassName", "ClassName" },
46+
{ "Version", "Version" }
47+
};
48+
49+
//DriverProcessor is the core implementation for driver updates and is only used for debugging; you don't need to write this code for normal use.
50+
/*var fileExtension = ".inf";
51+
var outPutPath = @"D:\drivers\";
52+
var driversPath = @"D:\driverslocal\";
53+
54+
var information = new DriverInformation.Builder()
55+
.SetDriverFileExtension(fileExtension)
56+
.SetOutPutDirectory(outPutPath)
57+
.SetDriverDirectory(driversPath)
58+
.SetFieldMappings(fieldMappingsCN)
59+
.Build();
60+
61+
var processor = new DriverProcessor();
62+
processor.AddCommand(new BackupDriverCommand(information));
63+
processor.AddCommand(new DeleteDriverCommand(information));
64+
processor.AddCommand(new InstallDriverCommand(information));
65+
processor.ProcessCommands();*/
66+
67+
Console.WriteLine($"升级程序初始化,{DateTime.Now}!");
68+
Console.WriteLine("当前运行目录:" + Thread.GetDomain().BaseDirectory);
69+
await Task.Delay(2000);
70+
await new GeneralUpdateBootstrap()
71+
//单个或多个更新包下载速度、剩余下载事件、当前下载版本信息通知事件
72+
.AddListenerMultiDownloadStatistics(OnMultiDownloadStatistics)
73+
//单个或多个更新包下载完成
74+
.AddListenerMultiDownloadCompleted(OnMultiDownloadCompleted)
75+
//完成所有的下载任务通知
76+
.AddListenerMultiAllDownloadCompleted(OnMultiAllDownloadCompleted)
77+
//下载过程出现的异常通知
78+
.AddListenerMultiDownloadError(OnMultiDownloadError)
79+
//整个更新过程出现的任何问题都会通过这个事件通知
80+
.AddListenerException(OnException)
81+
//设置字段映射表,用于解析所有驱动包的信息的字符串
82+
.SetFieldMappings(fieldMappingsCN)
83+
//是否开启驱动更新
84+
.Option(UpdateOption.Drive, true)
85+
.LaunchAsync();
86+
Console.WriteLine($"升级程序已启动,{DateTime.Now}!");
87+
}
88+
catch (Exception e)
89+
{
90+
Console.WriteLine(e.Message + "\n" + e.StackTrace);
91+
}
92+
93+
while (true)
94+
{
95+
var input = Console.ReadLine();
96+
if (input == "exit")
97+
{
98+
break;
99+
}
100+
}
101+
}
102+
103+
private static void OnMultiDownloadError(object arg1, MultiDownloadErrorEventArgs arg2)
104+
{
105+
var version = arg2.Version as VersionInfo;
106+
Console.WriteLine($"{version.Version} {arg2.Exception}");
107+
}
108+
109+
private static void OnMultiAllDownloadCompleted(object arg1, MultiAllDownloadCompletedEventArgs arg2)
110+
{
111+
Console.WriteLine(arg2.IsAllDownloadCompleted ? "所有的下载任务已完成!" : $"下载任务已失败!{arg2.FailedVersions.Count}");
112+
}
113+
114+
private static void OnMultiDownloadCompleted(object arg1, MultiDownloadCompletedEventArgs arg2)
115+
{
116+
var version = arg2.Version as VersionInfo;
117+
Console.WriteLine(arg2.IsComplated ? $"当前下载版本:{version.Version}, 下载完成!" : $"当前下载版本:{version.Version}, 下载失败!");
118+
}
119+
120+
private static void OnMultiDownloadStatistics(object arg1, MultiDownloadStatisticsEventArgs arg2)
121+
{
122+
var version = arg2.Version as VersionInfo;
123+
Console.WriteLine($"当前下载版本:{version.Version},下载速度:{arg2.Speed},剩余下载时间:{arg2.Remaining},已下载大小:{arg2.BytesReceived},总大小:{arg2.TotalBytesToReceive}, 进度百分比:{arg2.ProgressPercentage}%");
124+
}
125+
126+
private static void OnException(object arg1, ExceptionEventArgs arg2)
127+
{
128+
Console.WriteLine($"{arg2.Exception}");
129+
}
130+
}
131+
}

0 commit comments

Comments
 (0)