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