Subversion Repositories Code-Repo

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
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();
78
                            progressForm.Show(); 
79
                        }
80
                    }
81
                    else
82
                    {
53 Kevin 83
                        MessageBox.Show("bootsect.exe is missing from the master image location","Error");
9 Kevin 84
                    }
85
                }
86
                else
87
                {
88
                    MessageBox.Show("Settings have not been changed yet.\nPlease verify settings before continuing.", "Warning!");
89
                }
90
            }
91
            catch (Exception e)
92
            {
53 Kevin 93
                //MessageBox.Show(e.ToString(), "Error");
94
                DebugText.appendText(e.ToString());
95
                MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
9 Kevin 96
            }
97
        }
98
        private static void bwThread_DoWork(object sender, DoWorkEventArgs e)
99
        {
100
            try
101
            {
102
                BackgroundWorker worker = sender as BackgroundWorker;
103
                //Reimage_USB_Drives.wipeUSBDrives(worker, e);
104
                Thread.Sleep(1000);
105
                // Reformats all SWAT USB Drives
106
                foreach (string drive in allSwatUSBDrivesx32)
107
                {
108
                    if (worker.CancellationPending)
109
                    {
110
                        e.Cancel = true;
111
                        break;
112
                    }
113
                    progressForm.updateStatus("Reformatting Drive " + drive);
114
                    Process proc = new Process();
115
                    proc.StartInfo.FileName = "DISKPART";
116
                    proc.StartInfo.UseShellExecute = false;
117
                    proc.StartInfo.RedirectStandardInput = true;
118
                    proc.StartInfo.CreateNoWindow = true;
119
                    proc.Start();
120
 
121
                    StreamWriter procInput = proc.StandardInput;
122
                    procInput.WriteLine("SELECT VOLUME=" + drive);
123
                    procInput.WriteLine("FORMAT FS=NTFS LABEL=\"SWAT Drive x32\" QUICK");
124
                    procInput.WriteLine("EXIT");
125
                    proc.WaitForExit();
126
                }
127
                foreach (string drive in allSwatUSBDrivesx64)
128
                {
129
                    if (worker.CancellationPending)
130
                    {
131
                        e.Cancel = true;
132
                        break;
133
                    }
134
                    progressForm.updateStatus("Reformatting Drive " + drive);
135
                    Process proc = new Process();
136
                    proc.StartInfo.FileName = "DISKPART";
137
                    proc.StartInfo.UseShellExecute = false;
138
                    proc.StartInfo.RedirectStandardInput = true;
139
                    proc.StartInfo.CreateNoWindow = true;
140
                    proc.Start();
141
 
142
                    StreamWriter procInput = proc.StandardInput;
143
                    procInput.WriteLine("SELECT VOLUME=" + drive);
144
                    procInput.WriteLine("FORMAT FS=NTFS LABEL=\"SWAT Drive x64\" QUICK");
145
                    procInput.WriteLine("EXIT");
146
                    proc.WaitForExit();
147
                }
148
                getDriveLetters();
149
 
150
                //Reimage_USB_Drives.callBootsect();
151
 
152
                // Runs bootsect.exe on each drive to install the bootloader
153
                foreach (string drive in allSwatUSBDrivesx32)
154
                {
155
                    if (worker.CancellationPending)
156
                    {
157
                        e.Cancel = true;
158
                        break;
159
                    }
160
                    progressForm.updateStatus("Writing Bootsector on Drive " + drive);
161
                    Process proc = new Process();
162
                    proc.StartInfo.FileName = Settings_Form.usbMasterx32CopyLocation + "\\boot\\bootsect.exe";
163
                    proc.StartInfo.Arguments = "/NT60 " + drive.Substring(0, 2) + " /FORCE";
164
                    proc.Start();
165
                    proc.WaitForExit();
166
                }
167
                foreach (string drive in allSwatUSBDrivesx64)
168
                {
169
                    if (worker.CancellationPending)
170
                    {
171
                        e.Cancel = true;
172
                        break;
173
                    }
174
                    progressForm.updateStatus("Writing Bootsector on Drive " + drive);
175
                    Process proc = new Process();
176
                    proc.StartInfo.FileName = Settings_Form.usbMasterx32CopyLocation + "\\boot\\bootsect.exe";
177
                    proc.StartInfo.Arguments = "/NT60 " + drive.Substring(0, 2) + " /FORCE";
178
                    proc.Start();
179
                    proc.WaitForExit();
180
                }
181
 
182
                //Reimage_USB_Drives.copyMasterImage();
183
 
184
                // Copies the master image to each USB Drive
185
                foreach (string drive in allSwatUSBDrivesx32)
186
                {
187
                    if (worker.CancellationPending)
188
                    {
189
                        e.Cancel = true;
190
                        break;
191
                    }
192
                    BackgroundWorker fileCopyThread = new BackgroundWorker();
193
                    backgroundWorkerList.Add(fileCopyThread);
194
                    fileCopyThread.WorkerSupportsCancellation = true;
195
                    fileCopyThread.DoWork += new DoWorkEventHandler(fileCopyThread32_DoWork);
196
                    fileCopyThread.RunWorkerCompleted += new RunWorkerCompletedEventHandler(fileCopyThread_RunWorkerCompleted);
197
                    fileCopyThread.RunWorkerAsync(drive);
198
                    //DirectoryCopy(Settings_Form.usbMasterx32CopyLocation, drive, true, worker, e);
199
                }
200
                foreach (string drive in allSwatUSBDrivesx64)
201
                {
202
                    if (worker.CancellationPending)
203
                    {
204
                        e.Cancel = true;
205
                        break;
206
                    }
207
                    BackgroundWorker fileCopyThread = new BackgroundWorker();
208
                    backgroundWorkerList.Add(fileCopyThread);
209
                    fileCopyThread.WorkerSupportsCancellation = true;
210
                    fileCopyThread.DoWork += new DoWorkEventHandler(fileCopyThread64_DoWork);
211
                    fileCopyThread.RunWorkerCompleted += new RunWorkerCompletedEventHandler(fileCopyThread_RunWorkerCompleted);
212
                    fileCopyThread.RunWorkerAsync(drive);
213
                    //DirectoryCopy(Settings_Form.usbMasterx64CopyLocation, drive, true, worker, e);
214
                }
215
            }
216
            catch (Exception ex)
217
            {
53 Kevin 218
                //MessageBox.Show(e.ToString(), "Error");
219
                DebugText.appendText(ex.ToString());
220
                MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
9 Kevin 221
            }
222
        }
223
        static void fileCopyThread32_DoWork(object sender, DoWorkEventArgs e)
224
        {
225
            BackgroundWorker worker = sender as BackgroundWorker;
226
            string drive = (string)e.Argument;
227
            DirectoryCopy(Settings_Form.usbMasterx32CopyLocation, drive, true, worker, e);
228
            progressForm.updateStatus("Finished Copying Files to " + drive);
229
        }
230
        static void fileCopyThread64_DoWork(object sender, DoWorkEventArgs e)
231
        {
232
            BackgroundWorker worker = sender as BackgroundWorker;
233
            string drive = (string)e.Argument;
234
            DirectoryCopy(Settings_Form.usbMasterx64CopyLocation, drive, true, worker, e);
235
            progressForm.updateStatus("Finished Copying Files to " + drive);
236
        }
237
        static void fileCopyThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
238
        {
239
            if (e.Cancelled)
240
            {
241
                threadsCancelled = true;
242
                bool threadsStillRunning = false;
243
                foreach (BackgroundWorker bw in backgroundWorkerList)
244
                {
245
                    if (bw.IsBusy)
246
                        threadsStillRunning = true;
247
                }
248
                if (threadsStillRunning)
249
                    allThreadsCompleted = false;
250
                else
251
                    allThreadsCompleted = true;
252
            }
253
            else
254
            {
255
                bool threadsStillRunning = false;
256
                foreach (BackgroundWorker bw in backgroundWorkerList)
257
                {
258
                    if (bw.IsBusy)
259
                        threadsStillRunning = true;
260
                }
261
                if (threadsStillRunning)
262
                    allThreadsCompleted = false;
263
                else
264
                    allThreadsCompleted = true;
265
            }
266
            if (threadsCancelled)
267
            {
268
                threadsRunning = false;
269
                MessageBox.Show("File Copy Operation Cancelled");
270
                progressForm.closeForm();
271
            }
272
            else if (allThreadsCompleted)
273
            {
274
                threadsRunning = false;
275
                endTime = DateTime.Now;
276
                MessageBox.Show("Operation was started at " + startTime.ToShortTimeString() + " and ended successfully at " + endTime.ToShortTimeString());
277
                progressForm.closeForm();
278
            }
279
        }
280
        private static void bwThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
281
        {
282
            if (e.Cancelled)
283
            {
284
                threadsRunning = false;
285
                MessageBox.Show("Operation was canceled");
286
                progressForm.Close();
287
            }
288
        }
289
        private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs, BackgroundWorker worker, DoWorkEventArgs e)
290
        {
291
            try
292
            {
293
                progressForm.updateStatus("Copying Files to " + destDirName);
294
                DirectoryInfo dir = new DirectoryInfo(sourceDirName);
295
                DirectoryInfo[] dirs = dir.GetDirectories();
296
 
297
                // If the destination directory does not exist, create it.
298
                if (!Directory.Exists(destDirName))
299
                {
300
                    Directory.CreateDirectory(destDirName);
301
                }
302
                // Get the file contents of the directory to copy.
303
                FileInfo[] files = dir.GetFiles();
304
                foreach (FileInfo file in files)
305
                {
306
                    if (worker.CancellationPending)
307
                    {
308
                        e.Cancel = true;
309
                        break;
310
                    }
311
                    // Create the path to the new copy of the file.
312
                    string tempPath = Path.Combine(destDirName, file.Name);
313
                    // Copy the file.
314
                    file.CopyTo(tempPath, false);
315
                }
316
                // If copySubDirs is true, copy the subdirectories.
317
                if (copySubDirs)
318
                {
319
                    foreach (DirectoryInfo subdir in dirs)
320
                    {
321
                        if (worker.CancellationPending)
322
                        {
323
                            e.Cancel = true;
324
                            break;
325
                        }
326
                        // Create the subdirectory.
327
                        string tempPath = Path.Combine(destDirName, subdir.Name);
328
                        // Copy the subdirectories.
329
                        DirectoryCopy(subdir.FullName, tempPath, copySubDirs, worker, e);
330
                    }
331
                }
332
            }
333
            catch (Exception ex)
334
            {
53 Kevin 335
                //MessageBox.Show(e.ToString(), "Error");
336
                DebugText.appendText(ex.ToString());
337
                MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
9 Kevin 338
            }
339
        }
340
    }
341
}