1
- using Microsoft . Win32 ;
1
+ using Microsoft . Win32 ;
2
2
using System ;
3
3
using System . Linq ;
4
4
using System . Security . Principal ;
5
+ using static WTG_Utility_DeviceInfo ;
5
6
6
7
namespace WTG_Utility . Functions
7
8
{
8
9
internal class IsAdmin
9
10
{
10
11
public static void IsAdministrator ( )
11
12
{
12
- WindowsIdentity identity = WindowsIdentity . GetCurrent ( ) ; //Privilege Check
13
- WindowsPrincipal principal = new WindowsPrincipal ( identity ) ;
14
- bool userGroup = principal . IsInRole ( WindowsBuiltInRole . Administrator ) ;
15
- if ( userGroup == true )
13
+ try
16
14
{
15
+ WindowsIdentity identity = WindowsIdentity . GetCurrent ( ) ; //Privilege Check
16
+ WindowsPrincipal principal = new WindowsPrincipal ( identity ) ;
17
+ bool userGroup = principal . IsInRole ( WindowsBuiltInRole . Administrator ) ;
18
+ if ( userGroup == true )
19
+ {
20
+ }
21
+ else
22
+ {
23
+ Console . WriteLine ( ) ;
24
+ Console . WriteLine ( "Need to elevate privileges to run wtgutil." ) ;
25
+ Console . WriteLine ( ) ;
26
+ Environment . Exit ( 1 ) ;
27
+ }
17
28
}
18
- else
29
+ catch ( Exception ex )
19
30
{
20
- Console . WriteLine ( ) ;
21
- Console . WriteLine ( "Need to elevate privileges to run wtgutil." ) ;
22
- Console . WriteLine ( ) ;
31
+ Console . WriteLine ( $ "Error: { ex . Message } ") ;
23
32
Environment . Exit ( 1 ) ;
24
33
}
25
34
}
@@ -29,91 +38,234 @@ internal class GetSettings
29
38
{
30
39
internal static void GetBootDriverFlags ( )
31
40
{
32
- RegistryKey getBDF = Registry . LocalMachine . OpenSubKey ( "SYSTEM\\ HardwareConfig\\ Current" ) ;
33
- int statusBDF = ( int ) getBDF . GetValue ( "BootDriverFlags" ) ;
34
- if ( statusBDF == 20 ) //BootDriverFlags Check
41
+ try
35
42
{
36
- Console . WriteLine ( " Boot from USB Devices: Supported" ) ;
37
- }
38
- else if ( statusBDF == 28 )
39
- {
40
- Console . WriteLine ( " Boot from USB Devices: Supported" ) ;
41
- }
42
- else if ( statusBDF == 0 )
43
- {
44
- Console . WriteLine ( " Boot from USB Devices: Unsupported" ) ;
43
+ RegistryKey getBDF = Registry . LocalMachine . OpenSubKey ( "SYSTEM\\ HardwareConfig\\ Current" ) ;
44
+ int statusBDF = ( int ) getBDF . GetValue ( "BootDriverFlags" ) ;
45
+ if ( statusBDF == 20 ) //BootDriverFlags Check
46
+ {
47
+ Console . WriteLine ( " Boot from USB Devices: Supported" ) ;
48
+ }
49
+ else if ( statusBDF == 28 )
50
+ {
51
+ Console . WriteLine ( " Boot from USB Devices: Supported" ) ;
52
+ }
53
+ else if ( statusBDF == 0 )
54
+ {
55
+ Console . WriteLine ( " Boot from USB Devices: Unsupported" ) ;
56
+ }
57
+ else
58
+ {
59
+ Console . WriteLine ( " Boot from USB Devices: Status unknown" ) ;
60
+ }
61
+ getBDF . Close ( ) ;
45
62
}
46
- else
63
+ catch ( Exception ex )
47
64
{
48
- Console . WriteLine ( " Boot from USB Devices: Status unknown " ) ;
65
+ Console . WriteLine ( $ " Boot from USB Devices: error occurred: { ex . Message } ") ;
49
66
}
50
- getBDF . Close ( ) ;
51
67
}
52
68
53
69
internal static void GetPortableOSFeature ( )
54
70
{
55
- RegistryKey getPOS = Registry . LocalMachine . OpenSubKey ( "SYSTEM\\ CurrentControlSet\\ Control" ) ;
56
- bool existPOS = ( getPOS . GetValueNames ( ) . Contains ( "PortableOperatingSystem" ) ) ;
57
- if ( existPOS == true )
71
+ try
58
72
{
73
+ RegistryKey getPOS = Registry . LocalMachine . OpenSubKey ( "SYSTEM\\ CurrentControlSet\\ Control" ) ;
74
+ bool existPOS = ( getPOS . GetValueNames ( ) . Contains ( "PortableOperatingSystem" ) ) ;
59
75
int statusPOS = ( int ) getPOS . GetValue ( "PortableOperatingSystem" ) ;
60
76
if ( statusPOS == 1 ) //PortableOS Check
61
77
{
62
- Console . WriteLine ( " WindowsToGo Features: Enabled" ) ;
78
+ Console . WriteLine ( " WindowsToGo Features: Enabled" ) ;
63
79
}
64
80
else if ( statusPOS == 0 )
65
81
{
66
- Console . WriteLine ( " WindowsToGo Features: Disabled" ) ;
82
+ Console . WriteLine ( " WindowsToGo Features: Disabled" ) ;
67
83
}
68
84
else
69
85
{
70
- Console . WriteLine ( " WindowsToGo Features: Status unknown" ) ;
86
+ Console . WriteLine ( " WindowsToGo Features: Status unknown" ) ;
71
87
}
88
+ getPOS . Close ( ) ;
72
89
}
73
- else
90
+ catch ( Exception ex )
74
91
{
75
- Console . WriteLine ( " WindowsToGo Feature: [ERR: The specified registry key does not exist] " ) ;
92
+ Console . WriteLine ( $ " WindowsToGo Features: error occurred: { ex . Message } ") ;
76
93
}
77
- getPOS . Close ( ) ;
78
94
}
79
95
80
96
internal static void GetPartmgrSettings ( )
81
97
{
82
- RegistryKey getPMGR = Registry . LocalMachine . OpenSubKey ( "SYSTEM\\ CurrentControlSet\\ Services\\ partmgr\\ Parameters" ) ;
83
- int statusPMGR = ( int ) getPMGR . GetValue ( "SanPolicy" ) ;
84
- if ( statusPMGR == 4 ) //Partmgr Check
98
+ try
99
+ {
100
+ RegistryKey getPMGR = Registry . LocalMachine . OpenSubKey ( "SYSTEM\\ CurrentControlSet\\ Services\\ partmgr\\ Parameters" ) ;
101
+ int statusPMGR = ( int ) getPMGR . GetValue ( "SanPolicy" ) ;
102
+ if ( statusPMGR == 4 ) //Partmgr Check
103
+ {
104
+ Console . WriteLine ( " Hide Local Disks: True" ) ;
105
+ }
106
+ else
107
+ {
108
+ Console . WriteLine ( " Hide Local Disks: False" ) ;
109
+ }
110
+ getPMGR . Close ( ) ;
111
+ }
112
+ catch ( Exception ex )
113
+ {
114
+ Console . WriteLine ( $ " Hide Local Disks: error occurred: { ex . Message } ") ;
115
+ }
116
+ }
117
+
118
+ internal static void GetUASPStatus ( string deviceInstancePath )
119
+ {
120
+ try
85
121
{
86
- Console . WriteLine ( " Hide Local Disks: True" ) ;
122
+ RegistryKey getUASP = Registry . LocalMachine . CreateSubKey ( "SYSTEM\\ CurrentControlSet\\ Enum\\ " + deviceInstancePath ) ;
123
+ RegistryKey getUASPDriver = Registry . LocalMachine . CreateSubKey ( "SYSTEM\\ CurrentControlSet\\ Services\\ UASPStor\\ " ) ;
124
+ if ( ( int ) getUASP . GetValue ( "Capabilities" ) == 0x00000094
125
+ && ( string ) getUASP . GetValue ( "DeviceDesc" ) == "@usbstor.inf,%genericbulkonly.devicedesc%;USB Mass Storage Device"
126
+ && ( string ) getUASP . GetValue ( "Mfg" ) == "@usbstor.inf,%generic.mfg%;Compatible USB storage device"
127
+ && ( string ) getUASP . GetValue ( "Service" ) == "USBSTOR" )
128
+ {
129
+ if ( ( string ) getUASPDriver . GetValue ( "ImagePath" ) == "\\ SystemRoot\\ System32\\ drivers\\ USBSTOR.SYS" )
130
+ {
131
+ Console . WriteLine ( " UASP Status: Disabled with UASPStor service modified" ) ;
132
+ }
133
+ else
134
+ {
135
+ Console . WriteLine ( " UASP Status: Disabled" ) ;
136
+ }
137
+ }
138
+ else if ( ( int ) getUASP . GetValue ( "Capabilities" ) == 0x00000094
139
+ || ( string ) getUASP . GetValue ( "DeviceDesc" ) == "@usbstor.inf,%genericbulkonly.devicedesc%;USB Mass Storage Device"
140
+ || ( string ) getUASP . GetValue ( "Mfg" ) == "@usbstor.inf,%generic.mfg%;Compatible USB storage device"
141
+ || ( string ) getUASP . GetValue ( "Service" ) == "USBSTOR" )
142
+ {
143
+ Console . WriteLine ( " UASP Status: Unknown" ) ;
144
+ }
145
+ else
146
+ {
147
+ if ( ( string ) getUASPDriver . GetValue ( "ImagePath" ) == "\\ SystemRoot\\ System32\\ drivers\\ USBSTOR.SYS" )
148
+ {
149
+ Console . WriteLine ( " UASP Status: Enabled with UASPStor service modified" ) ;
150
+ }
151
+ else
152
+ {
153
+ Console . WriteLine ( " UASP Status: Enabled" ) ;
154
+ }
155
+ }
87
156
}
88
- else
157
+ catch ( Exception ex )
89
158
{
90
- Console . WriteLine ( " Hide Local Disks: False" ) ;
159
+ if ( ex . GetType ( ) == typeof ( NullReferenceException ) )
160
+ {
161
+ Console . WriteLine ( $ " UASP Status: error occurred. Please ensure that the WTG drive is plugged in.") ;
162
+ }
163
+ }
164
+ }
165
+
166
+ internal static string GetWTGDriveInstancePath ( )
167
+ {
168
+ try
169
+ {
170
+ return FindScsiStorageDevices ( ) ;
171
+ }
172
+ catch ( Exception ex )
173
+ {
174
+ Console . WriteLine ( $ "An error occured while getting device instance path: { ex . Message } ") ;
175
+ return string . Empty ;
91
176
}
92
- getPMGR . Close ( ) ;
93
177
}
94
178
}
95
179
96
180
internal class ModifySettings
97
181
{
98
182
internal static void SetBootDriverFlags ( int value )
99
183
{
100
- RegistryKey setBDF = Registry . LocalMachine . CreateSubKey ( "SYSTEM\\ HardwareConfig\\ Current" ) ;
101
- setBDF . SetValue ( "BootDriverFlags" , value , RegistryValueKind . DWord ) ;
102
- setBDF . Close ( ) ;
184
+ try
185
+ {
186
+ RegistryKey setBDF = Registry . LocalMachine . CreateSubKey ( "SYSTEM\\ HardwareConfig\\ Current" ) ;
187
+ setBDF . SetValue ( "BootDriverFlags" , value , RegistryValueKind . DWord ) ;
188
+ setBDF . Close ( ) ;
189
+ }
190
+ catch ( Exception ex )
191
+ {
192
+ Console . WriteLine ( $ "An error occured while modifying BootDriverFlags: { ex . Message } ") ;
193
+ }
103
194
}
104
195
105
196
internal static void SetPortableOSFeature ( int value )
106
197
{
107
- RegistryKey setPOS = Registry . LocalMachine . CreateSubKey ( "SYSTEM\\ CurrentControlSet\\ Control" ) ;
108
- setPOS . SetValue ( "PortableOperatingSystem" , value , RegistryValueKind . DWord ) ;
109
- setPOS . Close ( ) ;
198
+ try
199
+ {
200
+ RegistryKey setPOS = Registry . LocalMachine . CreateSubKey ( "SYSTEM\\ CurrentControlSet\\ Control" ) ;
201
+ setPOS . SetValue ( "PortableOperatingSystem" , value , RegistryValueKind . DWord ) ;
202
+ setPOS . Close ( ) ;
203
+ }
204
+ catch ( Exception ex )
205
+ {
206
+ Console . WriteLine ( $ "An error occured while modifying portable operating system features: { ex . Message } ") ;
207
+ }
110
208
}
111
209
112
210
internal static void SetPartmgrSettings ( int value )
113
211
{
114
- RegistryKey setPMGR = Registry . LocalMachine . CreateSubKey ( "SYSTEM\\ CurrentControlSet\\ Services\\ partmgr\\ Parameters" ) ;
115
- setPMGR . SetValue ( "SanPolicy" , value , RegistryValueKind . DWord ) ;
116
- setPMGR . Close ( ) ;
212
+ try
213
+ {
214
+ RegistryKey setPMGR = Registry . LocalMachine . CreateSubKey ( "SYSTEM\\ CurrentControlSet\\ Services\\ partmgr\\ Parameters" ) ;
215
+ setPMGR . SetValue ( "SanPolicy" , value , RegistryValueKind . DWord ) ;
216
+ setPMGR . Close ( ) ;
217
+ }
218
+ catch ( Exception ex )
219
+ {
220
+ Console . WriteLine ( $ "An error occured while modifying partmgr sanpolicy: { ex . Message } ") ;
221
+ }
222
+ }
223
+
224
+ internal static void DisableUASP ( string deviceInstancePath )
225
+ {
226
+ try
227
+ {
228
+ RegistryKey disableUASP = Registry . LocalMachine . CreateSubKey ( "SYSTEM\\ CurrentControlSet\\ Enum\\ " + deviceInstancePath ) ;
229
+ disableUASP . SetValue ( "Capabilities" , 0x00000094 , RegistryValueKind . DWord ) ;
230
+ disableUASP . SetValue ( "DeviceDesc" , "@usbstor.inf,%genericbulkonly.devicedesc%;USB Mass Storage Device" , RegistryValueKind . String ) ;
231
+ disableUASP . SetValue ( "Mfg" , "@usbstor.inf,%generic.mfg%;Compatible USB storage device" , RegistryValueKind . String ) ;
232
+ disableUASP . SetValue ( "Service" , "USBSTOR" , RegistryValueKind . String ) ;
233
+ disableUASP . Close ( ) ;
234
+ }
235
+ catch ( Exception ex )
236
+ {
237
+ Console . WriteLine ( $ "An error occured while disabling UASP: { ex . Message } ") ;
238
+ }
239
+ }
240
+
241
+ internal static void DisableUASPbyReplaceDriverConfig ( )
242
+ {
243
+ try
244
+ {
245
+ RegistryKey disableUASPbyReplaceDriverConfig = Registry . LocalMachine . CreateSubKey ( "SYSTEM\\ CurrentControlSet\\ Services\\ UASPStor\\ " ) ;
246
+ disableUASPbyReplaceDriverConfig . SetValue ( "ImagePath" , "\\ SystemRoot\\ System32\\ drivers\\ USBSTOR.SYS" , RegistryValueKind . String ) ;
247
+ disableUASPbyReplaceDriverConfig . SetValue ( "Owners" , "usbstor.inf\r \n v_mscdsc.inf\r \n " , RegistryValueKind . String ) ;
248
+ disableUASPbyReplaceDriverConfig . Close ( ) ;
249
+ }
250
+ catch ( Exception ex )
251
+ {
252
+ Console . WriteLine ( $ "An error occured while disabling UASP by driver config: { ex . Message } ") ;
253
+ }
254
+ }
255
+
256
+ internal static void RestoreUASPDriverConfig ( )
257
+ {
258
+ try
259
+ {
260
+ RegistryKey RestoreUASPDriverConfig = Registry . LocalMachine . CreateSubKey ( "SYSTEM\\ CurrentControlSet\\ Services\\ UASPStor\\ " ) ;
261
+ RestoreUASPDriverConfig . SetValue ( "ImagePath" , "\\ SystemRoot\\ System32\\ drivers\\ uaspstor.sys" , RegistryValueKind . String ) ;
262
+ RestoreUASPDriverConfig . SetValue ( "Owners" , "uaspstor.inf\r \n " , RegistryValueKind . String ) ;
263
+ RestoreUASPDriverConfig . Close ( ) ;
264
+ }
265
+ catch ( Exception ex )
266
+ {
267
+ Console . WriteLine ( $ "An error occured while restoring UASP driver config: { ex . Message } ") ;
268
+ }
117
269
}
118
270
}
119
271
}
0 commit comments