Subversion Repositories Code-Repo

Rev

Rev 63 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
46 Kevin 1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Linq;
7
using System.Text;
8
using System.Windows.Forms;
9
using System.IO;
10
using System.Threading;
11
 
12
namespace SWAT_Office_App
13
{
14
    public struct DriveEntry
15
    {
16
        public DateTime time;   // Time of insertion
17
        public string drive;    // Drive letter of the drive
18
        public string label;    // Label of the drive
19
        public string size;     // Size of the drive in bytes (I think)
20
        public string owner;    // Owner to be specified
21
        public string dock;     // Dock number as choosen
22
    }
23
    public partial class DriveLogger_Form : Form
24
    {
25
        private static List<DriveEntry> driveEntryList= new List<DriveEntry>();
26
        private static string logLocation = "DriveLogger Log.txt";
27
        public static bool instanceAlreadyRunning = false;
28
 
29
        public DriveLogger_Form()
30
        {
31
            // Disable thread safe checking
32
            CheckForIllegalCrossThreadCalls = false;
48 Kevin 33
 
46 Kevin 34
            driveEntryList.Clear();
35
            InitializeComponent();
36
 
37
            this.listView_Drives.AfterLabelEdit += new LabelEditEventHandler(listView_Drives_AfterLabelEdit);
38
            this.KeyPreview = true;
39
            this.KeyDown += new KeyEventHandler(DriveLogger_Form_KeyDown);
40
            this.FormClosed += new FormClosedEventHandler(DriveLogger_Form_FormClosed);
41
 
42
            using (StreamWriter sw = File.AppendText(logLocation))
43
            {
44
                sw.WriteLine("-- New Session Started --");
45
                sw.WriteLine("Initializing form details");
46
            }
47
 
48
            // Adds columns to the listview
49
            this.listView_Drives.Columns.Add("Owner", 145, HorizontalAlignment.Left);
50
            this.listView_Drives.Columns.Add("Dock", 60, HorizontalAlignment.Center);
51
            this.listView_Drives.Columns.Add("Time", 130, HorizontalAlignment.Center);
52
            this.listView_Drives.Columns.Add("Drive", 40, HorizontalAlignment.Center);
53
            this.listView_Drives.Columns.Add("Label", 109, HorizontalAlignment.Center);
54
            this.listView_Drives.Columns.Add("Size", 60, HorizontalAlignment.Center);
55
 
56
            // Initializes and starts a backgroundworker thread for monitoring for new drives
57
            BackgroundWorker bgWorker = new BackgroundWorker();
58
            instanceAlreadyRunning = true;
59
            bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
60
            bgWorker.RunWorkerAsync();
61
        }
62
        void bgWorker_DoWork(object sender, DoWorkEventArgs e)
63
        {
48 Kevin 64
            BackgroundWorker worker = sender as BackgroundWorker;
46 Kevin 65
            // Compares a list of drives detected previously with the a list of current
66
            // drives on the computer. If a change in length is detected, a drive is either
67
            // added or removed so the drive in question is found and operated upon
48 Kevin 68
            List<string> drivesPreviouslyDetectedList = new List<string>();
46 Kevin 69
            DriveInfo[] newlyDetectedDrives = DriveInfo.GetDrives();
70
 
71
            // Resets both lists to hold the same drives
48 Kevin 72
            drivesPreviouslyDetectedList.Clear();
46 Kevin 73
            foreach (DriveInfo drive in newlyDetectedDrives)
48 Kevin 74
                drivesPreviouslyDetectedList.Add(drive.Name);
46 Kevin 75
 
76
            while (true)
77
            {
48 Kevin 78
                // Cancels thread if DriveLogger form is closed
79
                if (!instanceAlreadyRunning)
80
                {
81
                    //MessageBox.Show("Thread Stopped");
82
                    e.Cancel = true;
83
                    break;
84
                }
85
                // Pulls new list of current drives on computer
46 Kevin 86
                newlyDetectedDrives = DriveInfo.GetDrives();
48 Kevin 87
                if (newlyDetectedDrives.Length > drivesPreviouslyDetectedList.Count)
46 Kevin 88
                {
89
                    // Applies if a drive is attached to the computer
90
                    // Goes through each list and finds the drive that was recently attached
91
                    bool newDrivesDetected = false;
92
                    string driveOwner = "";
93
                    string driveDock = "";
94
                    foreach (DriveInfo drive in newlyDetectedDrives)
95
                    {
48 Kevin 96
                        // Skips the drive if it is in the list of drives to ignore
97
                        if (Settings_Form.driveLoggerDrivesToIgnore.Contains(drive.Name))
98
                            continue;
99
 
50 Kevin 100
                        // Check for non-matching entries
48 Kevin 101
                        if (!drivesPreviouslyDetectedList.Contains(drive.Name))
46 Kevin 102
                        {
103
                            // Creates and populates a new DriveEntry
104
                            DriveEntry newEntry = new DriveEntry();
105
                            if (newDrivesDetected == false)
106
                            {
107
                                // First entry detected is prompted for dock label and owner name
108
                                newDrivesDetected = true;
109
                                DriveLogger_LabelPrompt_Form newPrompt = new DriveLogger_LabelPrompt_Form();
110
                                newPrompt.ShowDialog();
111
                                newEntry.owner = newPrompt.driveOwner;
112
                                driveOwner = newPrompt.driveOwner;
113
                                newEntry.dock = newPrompt.driveDock;
114
                                driveDock = newPrompt.driveDock;
115
                            }
116
                            else
117
                            {
118
                                // Subsequent entries use the previously entered dock label and owner
119
                                newDrivesDetected = true;
120
                                newEntry.owner = driveOwner;
121
                                newEntry.dock = driveDock;
122
                            }
123
 
124
                            // Sets other drive details for the drive
125
                            newEntry.time = DateTime.Now;
126
                            newEntry.drive = drive.Name;
127
 
128
                            DriveInfo tempDrive = null;
129
                            DriveInfo[] allDrives = DriveInfo.GetDrives();
130
                            foreach (DriveInfo driveSearch in allDrives)
131
                            {
132
                                if (driveSearch.IsReady)
133
                                {
134
                                    if (driveSearch.Name == newEntry.drive)
135
                                    {
136
                                        tempDrive = driveSearch;
137
                                        break;
138
                                    }
139
                                }
140
                            }
141
                            newEntry.label = tempDrive.VolumeLabel;
142
 
143
                            // Determines the size of the attached drive
144
                            if ((tempDrive.TotalSize / 1073741824) > 0)
145
                                newEntry.size = (tempDrive.TotalSize / 1073741824).ToString() + " GB";
146
                            else
147
                                newEntry.size = (tempDrive.TotalSize / 1048576).ToString() + " MB";
148
 
149
                            // Adds the new DriveEntry into driveList
150
                            driveEntryList.Add(newEntry);
151
 
152
                            using (StreamWriter sw = File.AppendText(logLocation))
153
                            {
154
                                sw.WriteLine("Drive Attached -- [" + newEntry.time.ToString() + "]\t\"" + newEntry.owner + "\"\t" + newEntry.drive + "\t\"" + newEntry.label + "\"\t" + newEntry.size);
155
                            }
156
 
157
                            paintDriveListbox();
158
                        }
159
                    }
160
                    // Clears and refreshes the drivesPreviouslyDetected list
48 Kevin 161
                    drivesPreviouslyDetectedList.Clear();
46 Kevin 162
                    foreach (DriveInfo drive in newlyDetectedDrives)
163
                    {
48 Kevin 164
                        drivesPreviouslyDetectedList.Add(drive.Name);
46 Kevin 165
                    }
166
                }
48 Kevin 167
                if (newlyDetectedDrives.Length < drivesPreviouslyDetectedList.Count)
46 Kevin 168
                {
169
                    // Applies if a drive is removed to the computer
170
                    // Goes through each list and finds the drive that was recently removed
171
                    bool removedDriveFound = false;
48 Kevin 172
                    foreach (string str in drivesPreviouslyDetectedList)
46 Kevin 173
                    {
48 Kevin 174
                        // Skips the drive if it is in the list of drives to ignore
175
                        if (Settings_Form.driveLoggerDrivesToIgnore.Contains(str))
176
                            continue;
177
 
46 Kevin 178
                        removedDriveFound = false;
179
                        // Loop here checks for non-matching entries in the two lists
180
                        foreach (DriveInfo drive in newlyDetectedDrives)
181
                        {
182
                            // If entries match, drive was not removed
183
                            if (str == drive.Name)
184
                            {
185
                                removedDriveFound = true;
186
                                break;
187
                            }
188
                        }
189
                        // If list mismatch is detected, remove from driveEntryList
190
                        if (removedDriveFound == false)
191
                        {
192
                            // Removes drive from driveList
193
                            foreach (DriveEntry entry in driveEntryList)
194
                            {
195
                                if (str == entry.drive)
196
                                {
197
                                    driveEntryList.Remove(entry);
198
                                    // Log the removal of the drive
199
                                    using (StreamWriter sw = File.AppendText(logLocation))
200
                                    {
201
                                        sw.WriteLine("Drive Removed --  [" + entry.time.ToString() + "]\t" + entry.drive + "\t\"" + entry.label + "\"\t" + entry.size);
202
                                    }
203
                                    break;
204
                                }
205
                            }
206
                            paintDriveListbox();
207
                        }
208
                    }
209
                    // Clears and refreshes the drivesPreviouslyDetected list
48 Kevin 210
                    drivesPreviouslyDetectedList.Clear();
46 Kevin 211
                    foreach (DriveInfo drive in newlyDetectedDrives)
212
                    {
48 Kevin 213
                        drivesPreviouslyDetectedList.Add(drive.Name);
46 Kevin 214
                    }
215
                }
48 Kevin 216
 
46 Kevin 217
                // Sleeps the thread for a specified amount of time
218
                Thread.Sleep(Settings_Form.driveLoggerRefreshInterval);
219
            }
220
        }
221
        void DriveLogger_Form_KeyDown(object sender, KeyEventArgs e)
222
        {
223
            // Monitors for keypresses for the about box and refresh
224
            switch (e.KeyCode)
225
            {
226
                case Keys.OemQuestion:
63 Kevin 227
                    About_Box_Form window = new About_Box_Form();
46 Kevin 228
                    window.ShowDialog();
229
                    break;
230
                case Keys.F5:
231
                    refreshDrives();
232
                    break;
233
            }
234
        }
235
        void listView_Drives_AfterLabelEdit(object sender, LabelEditEventArgs e)
236
        {
237
            // Logs the new label if it is changed
238
            if (e.Label != null)
239
            {
240
                using (StreamWriter sw = File.AppendText(logLocation))
241
                {
242
                    ListViewItem entry = listView_Drives.Items[e.Item];
243
 
244
                    sw.WriteLine("Label \"" + e.Label + "\" added to drive " + entry.SubItems[3].Text);
245
                }
246
            }
247
        }
248
        void DriveLogger_Form_FormClosed(object sender, FormClosedEventArgs e)
249
        {
250
            // Resets variable so form can be opened again
251
            instanceAlreadyRunning = false;
252
        }
253
        private void paintDriveListbox()
254
        {
255
            // Updates the listview
256
            this.listView_Drives.BeginUpdate();
257
 
258
            this.listView_Drives.Items.Clear();
259
 
260
            // Adds each entry from the driveList into the listView
261
            foreach (DriveEntry entry in driveEntryList)
262
            {
263
                if (entry.owner != "System Reserved")
264
                {
265
                    ListViewItem item = new ListViewItem();
266
                    item.Text = entry.owner;
267
                    ListViewItem.ListViewSubItem subDock = new ListViewItem.ListViewSubItem();
268
                    subDock.Text = entry.dock;
269
                    item.SubItems.Add(subDock);
270
                    ListViewItem.ListViewSubItem subTime = new ListViewItem.ListViewSubItem();
271
                    subTime.Text = entry.time.ToString();
272
                    item.SubItems.Add(subTime);
273
                    ListViewItem.ListViewSubItem subDrive = new ListViewItem.ListViewSubItem();
274
                    subDrive.Text = entry.drive;
275
                    item.SubItems.Add(subDrive);
276
                    ListViewItem.ListViewSubItem subLabel = new ListViewItem.ListViewSubItem();
277
                    subLabel.Text = entry.label;
278
                    item.SubItems.Add(subLabel);
279
                    ListViewItem.ListViewSubItem subSize = new ListViewItem.ListViewSubItem();
280
                    subSize.Text = entry.size;
281
                    item.SubItems.Add(subSize);
282
 
283
                    this.listView_Drives.Items.Add(item);
284
                }
285
            }
286
 
287
            this.listView_Drives.EndUpdate();
288
        }
289
        private void refreshDrives()
290
        {
291
            // Checks list of drive entries to see if any drives have been removed
292
            bool removedDriveFound;
293
            List<DriveEntry> drivesToRemove = new List<DriveEntry>();
294
            // Checks each entry in driveList to see if matching drive is found on list
295
            // of current drives. Removes entry from driveList if drive is not found.
296
            DriveInfo[] currentDrives = DriveInfo.GetDrives();
297
            for (int i = 0; i < driveEntryList.Count; i++)
298
            {
299
                DriveEntry storedDrive = driveEntryList[i];
300
                removedDriveFound = false;
301
                // Loop here checks for non-matching entries in the two lists
302
                foreach (DriveInfo currentDrive in currentDrives)
303
                {
304
                    // If entries match, drive was not removed
305
                    if (storedDrive.drive == currentDrive.Name)
306
                    {
307
                        removedDriveFound = true;
308
                        break;
309
                    }
310
                }
311
                // If list mismatch is detected, remove from driveList
312
                if (removedDriveFound == false)
313
                {
314
                    drivesToRemove.Add(storedDrive);
315
                }
316
            }
317
            // Removes drive from driveList
318
            foreach (DriveEntry entry in drivesToRemove)
319
            {
320
                // Removes drive from driveList
321
                foreach (DriveEntry entry2 in driveEntryList)
322
                {
323
                    if (entry.drive == entry2.drive)
324
                    {
325
                        driveEntryList.Remove(entry);
326
                        break;
327
                    }
328
                }
329
            }
330
 
331
            paintDriveListbox();
332
        }
333
    }
334
}