1- using System . IO ;
1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Diagnostics ;
4+ using System . IO ;
5+ using System . Linq ;
26using System . Text ;
37
48namespace GeneralUpdate . Core . Driver
59{
610 /// <summary>
711 /// When the /export-driver command backs up a driver, it backs up the driver package along with all its dependencies, such as associated library files and other related files.
812 /// </summary>
9- public class BackupDriverCommand : IDriverCommand
13+ public class BackupDriverCommand ( DriverInformation information ) : DriverCommand
1014 {
11- private DriverInformation _information ;
15+ private readonly string _driverExtension = $ "* { information . DriverFileExtension } " ;
1216
13- public BackupDriverCommand ( DriverInformation information ) => _information = information ;
14-
15- public void Execute ( )
17+ public override void Execute ( )
1618 {
19+ var uninstalledDrivers = Directory . GetFiles ( information . DriverDirectory , _driverExtension , SearchOption . AllDirectories ) . ToList ( ) ;
20+ var installedDrivers = GetInstalledDrivers ( information . FieldMappings ) ;
21+ var tempDrivers = installedDrivers . Where ( a => uninstalledDrivers . Any ( b => string . Equals ( a . OriginalName , Path . GetFileName ( b ) ) ) ) . ToList ( ) ;
22+ information . Drivers = tempDrivers ;
23+
24+ //Export the backup according to the driver name.
25+ if ( Directory . Exists ( information . OutPutDirectory ) )
26+ {
27+ Directory . Delete ( information . OutPutDirectory , true ) ;
28+ }
29+
30+ Directory . CreateDirectory ( information . OutPutDirectory ) ;
31+
1732 /*
1833 * Back up the specified list of drives.
1934 */
20- foreach ( var driver in _information . Drivers )
35+ foreach ( var driver in tempDrivers )
2136 {
22- //Export the backup according to the driver name.
23- if ( Directory . Exists ( _information . OutPutDirectory ) )
24- Directory . Delete ( _information . OutPutDirectory , true ) ;
25-
26- Directory . CreateDirectory ( _information . OutPutDirectory ) ;
2737 /*
2838 * If no test driver files are available, you can run the following command to export all installed driver files.
2939 * (1) dism /online /export-driver /destination:"D:\packet\cache\"
@@ -32,14 +42,82 @@ public void Execute()
3242 * The following code example exports the specified driver to the specified directory.
3343 * pnputil /export-driver oemXX.inf D:\packet\cache
3444 */
45+ var path = Path . Combine ( information . OutPutDirectory , driver . PublishedName ) ;
3546 var command = new StringBuilder ( "/c pnputil /export-driver " )
36- . Append ( driver )
47+ . Append ( driver . PublishedName )
3748 . Append ( ' ' )
38- . Append ( _information . OutPutDirectory )
49+ . Append ( path )
3950 . ToString ( ) ;
40-
51+
52+ if ( ! Directory . Exists ( path ) )
53+ {
54+ Directory . CreateDirectory ( path ) ;
55+ }
56+
4157 CommandExecutor . ExecuteCommand ( command ) ;
4258 }
4359 }
60+
61+ private IEnumerable < DriverInfo > GetInstalledDrivers ( Dictionary < string , string > fieldMappings )
62+ {
63+ var drivers = new List < DriverInfo > ( ) ;
64+ var process = new Process ( ) ;
65+ process . StartInfo . FileName = "pnputil" ;
66+ process . StartInfo . Arguments = "/enum-drivers" ;
67+ process . StartInfo . RedirectStandardOutput = true ;
68+ process . StartInfo . UseShellExecute = false ;
69+ process . StartInfo . CreateNoWindow = true ;
70+ process . Start ( ) ;
71+
72+ var output = process . StandardOutput . ReadToEnd ( ) ;
73+ process . WaitForExit ( ) ;
74+
75+ var lines = output . Split ( new [ ] { Environment . NewLine } , StringSplitOptions . RemoveEmptyEntries ) ;
76+
77+ DriverInfo currentDriver = null ;
78+ foreach ( var line in lines )
79+ {
80+ if ( line . StartsWith ( fieldMappings [ "PublishedName" ] ) )
81+ {
82+ if ( currentDriver != null )
83+ {
84+ drivers . Add ( currentDriver ) ;
85+ }
86+ currentDriver = new ( ) ;
87+ currentDriver . PublishedName = line . Split ( new [ ] { ':' } , 2 ) [ 1 ] . Trim ( ) ;
88+ }
89+ else if ( line . StartsWith ( fieldMappings [ "OriginalName" ] ) && currentDriver != null )
90+ {
91+ currentDriver . OriginalName = line . Split ( new [ ] { ':' } , 2 ) [ 1 ] . Trim ( ) ;
92+ }
93+ else if ( line . StartsWith ( fieldMappings [ "Provider" ] ) && currentDriver != null )
94+ {
95+ currentDriver . Provider = line . Split ( new [ ] { ':' } , 2 ) [ 1 ] . Trim ( ) ;
96+ }
97+ else if ( line . StartsWith ( fieldMappings [ "ClassName" ] ) && currentDriver != null )
98+ {
99+ currentDriver . ClassName = line . Split ( new [ ] { ':' } , 2 ) [ 1 ] . Trim ( ) ;
100+ }
101+ else if ( line . StartsWith ( fieldMappings [ "ClassGUID" ] ) && currentDriver != null )
102+ {
103+ currentDriver . ClassGUID = line . Split ( new [ ] { ':' } , 2 ) [ 1 ] . Trim ( ) ;
104+ }
105+ else if ( line . StartsWith ( fieldMappings [ "Version" ] ) && currentDriver != null )
106+ {
107+ currentDriver . Version = line . Split ( new [ ] { ':' } , 2 ) [ 1 ] . Trim ( ) ;
108+ }
109+ else if ( line . StartsWith ( fieldMappings [ "Signer" ] ) && currentDriver != null )
110+ {
111+ currentDriver . Signer = line . Split ( new [ ] { ':' } , 2 ) [ 1 ] . Trim ( ) ;
112+ }
113+ }
114+
115+ if ( currentDriver != null )
116+ {
117+ drivers . Add ( currentDriver ) ;
118+ }
119+
120+ return drivers ;
121+ }
44122 }
45123}
0 commit comments