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