Skip to content

Commit 021dda6

Browse files
committed
Minor adjustments
1 parent a5f5a73 commit 021dda6

File tree

8 files changed

+693
-113
lines changed

8 files changed

+693
-113
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ mono_crash.*
2121
[Dd]ebugPublic/
2222
[Rr]elease/
2323
[Rr]eleases/
24+
[Bb]in/
2425
x64/
2526
x86/
2627
[Ww][Ii][Nn]32/
@@ -32,6 +33,8 @@ bld/
3233
[Oo]ut/
3334
[Ll]og/
3435
[Ll]ogs/
36+
[Pp]ublish/
37+
[Ll]ogFiles/
3538

3639
# Visual Studio 2015/2017 cache/options directory
3740
.vs/
@@ -49,6 +52,7 @@ Generated\ Files/
4952
*.VisualState.xml
5053
TestResult.xml
5154
nunit-*.xml
55+
*.pfx
5256

5357
# Build Results of an ATL Project
5458
[Dd]ebugPS/

wtgutil/Functions.cs

Lines changed: 201 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,34 @@
1-
using Microsoft.Win32;
1+
using Microsoft.Win32;
22
using System;
33
using System.Linq;
44
using System.Security.Principal;
5+
using static WTG_Utility_DeviceInfo;
56

67
namespace WTG_Utility.Functions
78
{
89
internal class IsAdmin
910
{
1011
public static void IsAdministrator()
1112
{
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
1614
{
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+
}
1728
}
18-
else
29+
catch (Exception ex)
1930
{
20-
Console.WriteLine();
21-
Console.WriteLine("Need to elevate privileges to run wtgutil.");
22-
Console.WriteLine();
31+
Console.WriteLine($"Error: {ex.Message}");
2332
Environment.Exit(1);
2433
}
2534
}
@@ -29,91 +38,234 @@ internal class GetSettings
2938
{
3039
internal static void GetBootDriverFlags()
3140
{
32-
RegistryKey getBDF = Registry.LocalMachine.OpenSubKey("SYSTEM\\HardwareConfig\\Current");
33-
int statusBDF = (int)getBDF.GetValue("BootDriverFlags");
34-
if (statusBDF == 20) //BootDriverFlags Check
41+
try
3542
{
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();
4562
}
46-
else
63+
catch (Exception ex)
4764
{
48-
Console.WriteLine(" Boot from USB Devices: Status unknown");
65+
Console.WriteLine($" Boot from USB Devices: error occurred: {ex.Message}");
4966
}
50-
getBDF.Close();
5167
}
5268

5369
internal static void GetPortableOSFeature()
5470
{
55-
RegistryKey getPOS = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Control");
56-
bool existPOS = (getPOS.GetValueNames().Contains("PortableOperatingSystem"));
57-
if (existPOS == true)
71+
try
5872
{
73+
RegistryKey getPOS = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Control");
74+
bool existPOS = (getPOS.GetValueNames().Contains("PortableOperatingSystem"));
5975
int statusPOS = (int)getPOS.GetValue("PortableOperatingSystem");
6076
if (statusPOS == 1) //PortableOS Check
6177
{
62-
Console.WriteLine(" WindowsToGo Features: Enabled");
78+
Console.WriteLine(" WindowsToGo Features: Enabled");
6379
}
6480
else if (statusPOS == 0)
6581
{
66-
Console.WriteLine(" WindowsToGo Features: Disabled");
82+
Console.WriteLine(" WindowsToGo Features: Disabled");
6783
}
6884
else
6985
{
70-
Console.WriteLine(" WindowsToGo Features: Status unknown");
86+
Console.WriteLine(" WindowsToGo Features: Status unknown");
7187
}
88+
getPOS.Close();
7289
}
73-
else
90+
catch (Exception ex)
7491
{
75-
Console.WriteLine(" WindowsToGo Feature: [ERR: The specified registry key does not exist]");
92+
Console.WriteLine($" WindowsToGo Features: error occurred: {ex.Message}");
7693
}
77-
getPOS.Close();
7894
}
7995

8096
internal static void GetPartmgrSettings()
8197
{
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
85121
{
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+
}
87156
}
88-
else
157+
catch (Exception ex)
89158
{
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;
91176
}
92-
getPMGR.Close();
93177
}
94178
}
95179

96180
internal class ModifySettings
97181
{
98182
internal static void SetBootDriverFlags(int value)
99183
{
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+
}
103194
}
104195

105196
internal static void SetPortableOSFeature(int value)
106197
{
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+
}
110208
}
111209

112210
internal static void SetPartmgrSettings(int value)
113211
{
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\nv_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+
}
117269
}
118270
}
119271
}

0 commit comments

Comments
 (0)