| 9 |
Kevin |
1 |
using System;
|
|
|
2 |
using System.Collections.Generic;
|
|
|
3 |
using System.Linq;
|
|
|
4 |
using System.Text;
|
|
|
5 |
using System.IO;
|
|
|
6 |
using System.Windows.Forms;
|
|
|
7 |
using System.Diagnostics;
|
|
|
8 |
using System.Threading;
|
|
|
9 |
using System.ComponentModel;
|
|
|
10 |
|
|
|
11 |
namespace SWAT_Office_App
|
|
|
12 |
{
|
|
|
13 |
class Reimage_USB_Drives
|
|
|
14 |
{
|
|
|
15 |
public static List<string> allSwatUSBDrivesx32 = new List<string>();
|
|
|
16 |
public static List<string> allSwatUSBDrivesx64 = new List<string>();
|
|
|
17 |
public static BackgroundWorker bwThread = null;
|
|
|
18 |
public static Reimage_USB_Drive_Progress_Form progressForm = null;
|
|
|
19 |
public static List<BackgroundWorker> backgroundWorkerList = new List<BackgroundWorker>();
|
|
|
20 |
public static bool threadsRunning = false;
|
|
|
21 |
private static bool allThreadsCompleted = false;
|
|
|
22 |
private static bool threadsCancelled = false;
|
|
|
23 |
private static DateTime startTime;
|
|
|
24 |
private static DateTime endTime;
|
|
|
25 |
public static void getDriveLetters()
|
|
|
26 |
{
|
|
|
27 |
allSwatUSBDrivesx32.Clear();
|
|
|
28 |
allSwatUSBDrivesx64.Clear();
|
|
|
29 |
backgroundWorkerList.Clear();
|
|
|
30 |
string pathToSWATDrive;
|
|
|
31 |
// Pulls information from all drives on the running computer
|
|
|
32 |
DriveInfo[] allDrives = DriveInfo.GetDrives();
|
|
|
33 |
// Looks for a drive with the correct label, updates pathToSWATDrive with the drive letter
|
|
|
34 |
foreach (DriveInfo drive in allDrives)
|
|
|
35 |
{
|
|
|
36 |
if (drive.IsReady == true)
|
|
|
37 |
{
|
|
|
38 |
// Drive label for the USB drives is set here.
|
|
|
39 |
if (drive.VolumeLabel.ToUpper() == "SWAT DRIVE X32" && drive.DriveType == DriveType.Removable)
|
|
|
40 |
{
|
|
|
41 |
pathToSWATDrive = drive.Name;
|
|
|
42 |
allSwatUSBDrivesx32.Add(pathToSWATDrive);
|
|
|
43 |
}
|
|
|
44 |
if (drive.VolumeLabel.ToUpper() == "SWAT DRIVE X64" && drive.DriveType == DriveType.Removable)
|
|
|
45 |
{
|
|
|
46 |
pathToSWATDrive = drive.Name;
|
|
|
47 |
allSwatUSBDrivesx64.Add(pathToSWATDrive);
|
|
|
48 |
}
|
|
|
49 |
}
|
|
|
50 |
}
|
|
|
51 |
// Prompts for drive to be inserted
|
|
|
52 |
if ((allSwatUSBDrivesx32.Count == 0) && (allSwatUSBDrivesx64.Count == 0))
|
|
|
53 |
{
|
|
|
54 |
DialogResult result;
|
|
|
55 |
result = MessageBox.Show("Unable to locate any SWAT USB drives.", "Error");
|
|
|
56 |
}
|
|
|
57 |
}
|
|
|
58 |
public static void reimageUSBDrives()
|
|
|
59 |
{
|
|
|
60 |
try
|
|
|
61 |
{
|
|
|
62 |
if (Settings_Form.defaultSettings == false)
|
|
|
63 |
{
|
|
|
64 |
if ((File.Exists(Settings_Form.usbMasterx32CopyLocation + "\\boot\\bootsect.exe")) &&
|
|
|
65 |
(File.Exists(Settings_Form.usbMasterx64CopyLocation + "\\boot\\bootsect.exe")))
|
|
|
66 |
{
|
|
|
67 |
startTime = DateTime.Now;
|
|
|
68 |
getDriveLetters();
|
|
|
69 |
if ((allSwatUSBDrivesx32.Count != 0) || (allSwatUSBDrivesx64.Count != 0))
|
|
|
70 |
{
|
|
|
71 |
threadsRunning = true;
|
|
|
72 |
bwThread = new BackgroundWorker();
|
|
|
73 |
bwThread.WorkerSupportsCancellation = true;
|
|
|
74 |
bwThread.DoWork += new DoWorkEventHandler(bwThread_DoWork);
|
|
|
75 |
bwThread.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bwThread_RunWorkerCompleted);
|
|
|
76 |
bwThread.RunWorkerAsync();
|
|
|
77 |
progressForm = new Reimage_USB_Drive_Progress_Form();
|
| 59 |
Kevin |
78 |
progressForm.Show();
|
| 63 |
Kevin |
79 |
Debug.appendText("Beginning the reimaging of USB Drives");
|
| 9 |
Kevin |
80 |
}
|
|
|
81 |
}
|
|
|
82 |
else
|
|
|
83 |
{
|
| 53 |
Kevin |
84 |
MessageBox.Show("bootsect.exe is missing from the master image location","Error");
|
| 9 |
Kevin |
85 |
}
|
|
|
86 |
}
|
|
|
87 |
else
|
|
|
88 |
{
|
|
|
89 |
MessageBox.Show("Settings have not been changed yet.\nPlease verify settings before continuing.", "Warning!");
|
|
|
90 |
}
|
|
|
91 |
}
|
|
|
92 |
catch (Exception e)
|
|
|
93 |
{
|
| 53 |
Kevin |
94 |
//MessageBox.Show(e.ToString(), "Error");
|
| 63 |
Kevin |
95 |
Debug.appendText(e.ToString());
|
| 53 |
Kevin |
96 |
MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
|
| 9 |
Kevin |
97 |
}
|
|
|
98 |
}
|
|
|
99 |
private static void bwThread_DoWork(object sender, DoWorkEventArgs e)
|
|
|
100 |
{
|
|
|
101 |
try
|
|
|
102 |
{
|
|
|
103 |
BackgroundWorker worker = sender as BackgroundWorker;
|
|
|
104 |
//Reimage_USB_Drives.wipeUSBDrives(worker, e);
|
|
|
105 |
Thread.Sleep(1000);
|
|
|
106 |
// Reformats all SWAT USB Drives
|
|
|
107 |
foreach (string drive in allSwatUSBDrivesx32)
|
|
|
108 |
{
|
|
|
109 |
if (worker.CancellationPending)
|
|
|
110 |
{
|
|
|
111 |
e.Cancel = true;
|
|
|
112 |
break;
|
|
|
113 |
}
|
|
|
114 |
progressForm.updateStatus("Reformatting Drive " + drive);
|
|
|
115 |
Process proc = new Process();
|
|
|
116 |
proc.StartInfo.FileName = "DISKPART";
|
|
|
117 |
proc.StartInfo.UseShellExecute = false;
|
|
|
118 |
proc.StartInfo.RedirectStandardInput = true;
|
|
|
119 |
proc.StartInfo.CreateNoWindow = true;
|
|
|
120 |
proc.Start();
|
|
|
121 |
|
|
|
122 |
StreamWriter procInput = proc.StandardInput;
|
|
|
123 |
procInput.WriteLine("SELECT VOLUME=" + drive);
|
|
|
124 |
procInput.WriteLine("FORMAT FS=NTFS LABEL=\"SWAT Drive x32\" QUICK");
|
|
|
125 |
procInput.WriteLine("EXIT");
|
|
|
126 |
proc.WaitForExit();
|
|
|
127 |
}
|
|
|
128 |
foreach (string drive in allSwatUSBDrivesx64)
|
|
|
129 |
{
|
|
|
130 |
if (worker.CancellationPending)
|
|
|
131 |
{
|
|
|
132 |
e.Cancel = true;
|
|
|
133 |
break;
|
|
|
134 |
}
|
|
|
135 |
progressForm.updateStatus("Reformatting Drive " + drive);
|
|
|
136 |
Process proc = new Process();
|
|
|
137 |
proc.StartInfo.FileName = "DISKPART";
|
|
|
138 |
proc.StartInfo.UseShellExecute = false;
|
|
|
139 |
proc.StartInfo.RedirectStandardInput = true;
|
|
|
140 |
proc.StartInfo.CreateNoWindow = true;
|
|
|
141 |
proc.Start();
|
|
|
142 |
|
|
|
143 |
StreamWriter procInput = proc.StandardInput;
|
|
|
144 |
procInput.WriteLine("SELECT VOLUME=" + drive);
|
|
|
145 |
procInput.WriteLine("FORMAT FS=NTFS LABEL=\"SWAT Drive x64\" QUICK");
|
|
|
146 |
procInput.WriteLine("EXIT");
|
|
|
147 |
proc.WaitForExit();
|
|
|
148 |
}
|
|
|
149 |
getDriveLetters();
|
|
|
150 |
|
|
|
151 |
//Reimage_USB_Drives.callBootsect();
|
|
|
152 |
|
|
|
153 |
// Runs bootsect.exe on each drive to install the bootloader
|
|
|
154 |
foreach (string drive in allSwatUSBDrivesx32)
|
|
|
155 |
{
|
|
|
156 |
if (worker.CancellationPending)
|
|
|
157 |
{
|
|
|
158 |
e.Cancel = true;
|
|
|
159 |
break;
|
|
|
160 |
}
|
|
|
161 |
progressForm.updateStatus("Writing Bootsector on Drive " + drive);
|
|
|
162 |
Process proc = new Process();
|
|
|
163 |
proc.StartInfo.FileName = Settings_Form.usbMasterx32CopyLocation + "\\boot\\bootsect.exe";
|
|
|
164 |
proc.StartInfo.Arguments = "/NT60 " + drive.Substring(0, 2) + " /FORCE";
|
|
|
165 |
proc.Start();
|
|
|
166 |
proc.WaitForExit();
|
|
|
167 |
}
|
|
|
168 |
foreach (string drive in allSwatUSBDrivesx64)
|
|
|
169 |
{
|
|
|
170 |
if (worker.CancellationPending)
|
|
|
171 |
{
|
|
|
172 |
e.Cancel = true;
|
|
|
173 |
break;
|
|
|
174 |
}
|
|
|
175 |
progressForm.updateStatus("Writing Bootsector on Drive " + drive);
|
|
|
176 |
Process proc = new Process();
|
|
|
177 |
proc.StartInfo.FileName = Settings_Form.usbMasterx32CopyLocation + "\\boot\\bootsect.exe";
|
|
|
178 |
proc.StartInfo.Arguments = "/NT60 " + drive.Substring(0, 2) + " /FORCE";
|
|
|
179 |
proc.Start();
|
|
|
180 |
proc.WaitForExit();
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
//Reimage_USB_Drives.copyMasterImage();
|
|
|
184 |
|
|
|
185 |
// Copies the master image to each USB Drive
|
|
|
186 |
foreach (string drive in allSwatUSBDrivesx32)
|
|
|
187 |
{
|
|
|
188 |
if (worker.CancellationPending)
|
|
|
189 |
{
|
|
|
190 |
e.Cancel = true;
|
|
|
191 |
break;
|
|
|
192 |
}
|
|
|
193 |
BackgroundWorker fileCopyThread = new BackgroundWorker();
|
|
|
194 |
backgroundWorkerList.Add(fileCopyThread);
|
|
|
195 |
fileCopyThread.WorkerSupportsCancellation = true;
|
|
|
196 |
fileCopyThread.DoWork += new DoWorkEventHandler(fileCopyThread32_DoWork);
|
|
|
197 |
fileCopyThread.RunWorkerCompleted += new RunWorkerCompletedEventHandler(fileCopyThread_RunWorkerCompleted);
|
|
|
198 |
fileCopyThread.RunWorkerAsync(drive);
|
|
|
199 |
//DirectoryCopy(Settings_Form.usbMasterx32CopyLocation, drive, true, worker, e);
|
|
|
200 |
}
|
|
|
201 |
foreach (string drive in allSwatUSBDrivesx64)
|
|
|
202 |
{
|
|
|
203 |
if (worker.CancellationPending)
|
|
|
204 |
{
|
|
|
205 |
e.Cancel = true;
|
|
|
206 |
break;
|
|
|
207 |
}
|
|
|
208 |
BackgroundWorker fileCopyThread = new BackgroundWorker();
|
|
|
209 |
backgroundWorkerList.Add(fileCopyThread);
|
|
|
210 |
fileCopyThread.WorkerSupportsCancellation = true;
|
|
|
211 |
fileCopyThread.DoWork += new DoWorkEventHandler(fileCopyThread64_DoWork);
|
|
|
212 |
fileCopyThread.RunWorkerCompleted += new RunWorkerCompletedEventHandler(fileCopyThread_RunWorkerCompleted);
|
|
|
213 |
fileCopyThread.RunWorkerAsync(drive);
|
|
|
214 |
//DirectoryCopy(Settings_Form.usbMasterx64CopyLocation, drive, true, worker, e);
|
|
|
215 |
}
|
|
|
216 |
}
|
|
|
217 |
catch (Exception ex)
|
|
|
218 |
{
|
| 53 |
Kevin |
219 |
//MessageBox.Show(e.ToString(), "Error");
|
| 63 |
Kevin |
220 |
Debug.appendText(ex.ToString());
|
| 53 |
Kevin |
221 |
MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
|
| 9 |
Kevin |
222 |
}
|
|
|
223 |
}
|
|
|
224 |
static void fileCopyThread32_DoWork(object sender, DoWorkEventArgs e)
|
|
|
225 |
{
|
|
|
226 |
BackgroundWorker worker = sender as BackgroundWorker;
|
|
|
227 |
string drive = (string)e.Argument;
|
|
|
228 |
DirectoryCopy(Settings_Form.usbMasterx32CopyLocation, drive, true, worker, e);
|
|
|
229 |
progressForm.updateStatus("Finished Copying Files to " + drive);
|
|
|
230 |
}
|
|
|
231 |
static void fileCopyThread64_DoWork(object sender, DoWorkEventArgs e)
|
|
|
232 |
{
|
|
|
233 |
BackgroundWorker worker = sender as BackgroundWorker;
|
|
|
234 |
string drive = (string)e.Argument;
|
|
|
235 |
DirectoryCopy(Settings_Form.usbMasterx64CopyLocation, drive, true, worker, e);
|
|
|
236 |
progressForm.updateStatus("Finished Copying Files to " + drive);
|
|
|
237 |
}
|
|
|
238 |
static void fileCopyThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
|
|
|
239 |
{
|
|
|
240 |
if (e.Cancelled)
|
|
|
241 |
{
|
|
|
242 |
threadsCancelled = true;
|
|
|
243 |
bool threadsStillRunning = false;
|
|
|
244 |
foreach (BackgroundWorker bw in backgroundWorkerList)
|
|
|
245 |
{
|
|
|
246 |
if (bw.IsBusy)
|
|
|
247 |
threadsStillRunning = true;
|
|
|
248 |
}
|
|
|
249 |
if (threadsStillRunning)
|
|
|
250 |
allThreadsCompleted = false;
|
|
|
251 |
else
|
|
|
252 |
allThreadsCompleted = true;
|
|
|
253 |
}
|
|
|
254 |
else
|
|
|
255 |
{
|
|
|
256 |
bool threadsStillRunning = false;
|
|
|
257 |
foreach (BackgroundWorker bw in backgroundWorkerList)
|
|
|
258 |
{
|
|
|
259 |
if (bw.IsBusy)
|
|
|
260 |
threadsStillRunning = true;
|
|
|
261 |
}
|
|
|
262 |
if (threadsStillRunning)
|
|
|
263 |
allThreadsCompleted = false;
|
|
|
264 |
else
|
|
|
265 |
allThreadsCompleted = true;
|
|
|
266 |
}
|
|
|
267 |
if (threadsCancelled)
|
|
|
268 |
{
|
|
|
269 |
threadsRunning = false;
|
|
|
270 |
MessageBox.Show("File Copy Operation Cancelled");
|
|
|
271 |
progressForm.closeForm();
|
|
|
272 |
}
|
|
|
273 |
else if (allThreadsCompleted)
|
|
|
274 |
{
|
|
|
275 |
threadsRunning = false;
|
|
|
276 |
endTime = DateTime.Now;
|
| 63 |
Kevin |
277 |
Debug.appendText("Reimaging of USB Drives started at " + startTime.ToShortTimeString() + " and ended at " + endTime.ToShortTimeString());
|
| 9 |
Kevin |
278 |
MessageBox.Show("Operation was started at " + startTime.ToShortTimeString() + " and ended successfully at " + endTime.ToShortTimeString());
|
|
|
279 |
progressForm.closeForm();
|
|
|
280 |
}
|
|
|
281 |
}
|
|
|
282 |
private static void bwThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
|
|
|
283 |
{
|
|
|
284 |
if (e.Cancelled)
|
|
|
285 |
{
|
|
|
286 |
threadsRunning = false;
|
| 63 |
Kevin |
287 |
Debug.appendText("USB reimaging operation has been cancelled");
|
| 9 |
Kevin |
288 |
MessageBox.Show("Operation was canceled");
|
|
|
289 |
progressForm.Close();
|
|
|
290 |
}
|
|
|
291 |
}
|
|
|
292 |
private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs, BackgroundWorker worker, DoWorkEventArgs e)
|
|
|
293 |
{
|
|
|
294 |
try
|
|
|
295 |
{
|
|
|
296 |
progressForm.updateStatus("Copying Files to " + destDirName);
|
|
|
297 |
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
|
|
|
298 |
DirectoryInfo[] dirs = dir.GetDirectories();
|
|
|
299 |
|
|
|
300 |
// If the destination directory does not exist, create it.
|
|
|
301 |
if (!Directory.Exists(destDirName))
|
|
|
302 |
{
|
|
|
303 |
Directory.CreateDirectory(destDirName);
|
|
|
304 |
}
|
|
|
305 |
// Get the file contents of the directory to copy.
|
|
|
306 |
FileInfo[] files = dir.GetFiles();
|
|
|
307 |
foreach (FileInfo file in files)
|
|
|
308 |
{
|
|
|
309 |
if (worker.CancellationPending)
|
|
|
310 |
{
|
|
|
311 |
e.Cancel = true;
|
|
|
312 |
break;
|
|
|
313 |
}
|
|
|
314 |
// Create the path to the new copy of the file.
|
|
|
315 |
string tempPath = Path.Combine(destDirName, file.Name);
|
|
|
316 |
// Copy the file.
|
|
|
317 |
file.CopyTo(tempPath, false);
|
|
|
318 |
}
|
|
|
319 |
// If copySubDirs is true, copy the subdirectories.
|
|
|
320 |
if (copySubDirs)
|
|
|
321 |
{
|
|
|
322 |
foreach (DirectoryInfo subdir in dirs)
|
|
|
323 |
{
|
|
|
324 |
if (worker.CancellationPending)
|
|
|
325 |
{
|
|
|
326 |
e.Cancel = true;
|
|
|
327 |
break;
|
|
|
328 |
}
|
|
|
329 |
// Create the subdirectory.
|
|
|
330 |
string tempPath = Path.Combine(destDirName, subdir.Name);
|
|
|
331 |
// Copy the subdirectories.
|
|
|
332 |
DirectoryCopy(subdir.FullName, tempPath, copySubDirs, worker, e);
|
|
|
333 |
}
|
|
|
334 |
}
|
|
|
335 |
}
|
|
|
336 |
catch (Exception ex)
|
|
|
337 |
{
|
| 53 |
Kevin |
338 |
//MessageBox.Show(e.ToString(), "Error");
|
| 63 |
Kevin |
339 |
Debug.appendText(ex.ToString());
|
| 53 |
Kevin |
340 |
MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
|
| 9 |
Kevin |
341 |
}
|
|
|
342 |
}
|
|
|
343 |
}
|
|
|
344 |
}
|