Subversion Repositories Code-Repo

Rev

Rev 68 | 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)
101 Kevin 20
        public string owner;    // Owner to be specified (optional)
46 Kevin 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();
101 Kevin 105
 
106
                            //if (newDrivesDetected == false)
107
                            //{
108
                            //    // First entry detected is prompted for dock label and owner name
109
                            //    newDrivesDetected = true;
110
                            //    DriveLogger_LabelPrompt_Form newPrompt = new DriveLogger_LabelPrompt_Form();
111
                            //    newPrompt.ShowDialog();
112
                            //    newEntry.owner = newPrompt.driveOwner;
113
                            //    driveOwner = newPrompt.driveOwner;
114
                            //    newEntry.dock = newPrompt.driveDock;
115
                            //    driveDock = newPrompt.driveDock;
116
                            //}
117
                            //else
118
                            //{
119
                            //    // Subsequent entries use the previously entered dock label and owner
120
                            //    newDrivesDetected = true;
121
                            //    newEntry.owner = driveOwner;
122
                            //    newEntry.dock = driveDock;
123
                            //}
46 Kevin 124
 
125
                            // Sets other drive details for the drive
126
                            newEntry.time = DateTime.Now;
127
                            newEntry.drive = drive.Name;
128
 
129
                            DriveInfo tempDrive = null;
130
                            DriveInfo[] allDrives = DriveInfo.GetDrives();
131
                            foreach (DriveInfo driveSearch in allDrives)
132
                            {
133
                                if (driveSearch.IsReady)
134
                                {
135
                                    if (driveSearch.Name == newEntry.drive)
136
                                    {
137
                                        tempDrive = driveSearch;
138
                                        break;
139
                                    }
140
                                }
141
                            }
142
                            newEntry.label = tempDrive.VolumeLabel;
143
 
144
                            // Determines the size of the attached drive
145
                            if ((tempDrive.TotalSize / 1073741824) > 0)
146
                                newEntry.size = (tempDrive.TotalSize / 1073741824).ToString() + " GB";
147
                            else
148
                                newEntry.size = (tempDrive.TotalSize / 1048576).ToString() + " MB";
149
 
150
                            // Adds the new DriveEntry into driveList
151
                            driveEntryList.Add(newEntry);
152
 
153
                            using (StreamWriter sw = File.AppendText(logLocation))
154
                            {
155
                                sw.WriteLine("Drive Attached -- [" + newEntry.time.ToString() + "]\t\"" + newEntry.owner + "\"\t" + newEntry.drive + "\t\"" + newEntry.label + "\"\t" + newEntry.size);
156
                            }
157
 
158
                            paintDriveListbox();
159
                        }
160
                    }
161
                    // Clears and refreshes the drivesPreviouslyDetected list
48 Kevin 162
                    drivesPreviouslyDetectedList.Clear();
46 Kevin 163
                    foreach (DriveInfo drive in newlyDetectedDrives)
164
                    {
48 Kevin 165
                        drivesPreviouslyDetectedList.Add(drive.Name);
46 Kevin 166
                    }
167
                }
48 Kevin 168
                if (newlyDetectedDrives.Length < drivesPreviouslyDetectedList.Count)
46 Kevin 169
                {
170
                    // Applies if a drive is removed to the computer
171
                    // Goes through each list and finds the drive that was recently removed
172
                    bool removedDriveFound = false;
48 Kevin 173
                    foreach (string str in drivesPreviouslyDetectedList)
46 Kevin 174
                    {
48 Kevin 175
                        // Skips the drive if it is in the list of drives to ignore
176
                        if (Settings_Form.driveLoggerDrivesToIgnore.Contains(str))
177
                            continue;
178
 
46 Kevin 179
                        removedDriveFound = false;
180
                        // Loop here checks for non-matching entries in the two lists
181
                        foreach (DriveInfo drive in newlyDetectedDrives)
182
                        {
183
                            // If entries match, drive was not removed
184
                            if (str == drive.Name)
185
                            {
186
                                removedDriveFound = true;
187
                                break;
188
                            }
189
                        }
190
                        // If list mismatch is detected, remove from driveEntryList
191
                        if (removedDriveFound == false)
192
                        {
193
                            // Removes drive from driveList
194
                            foreach (DriveEntry entry in driveEntryList)
195
                            {
196
                                if (str == entry.drive)
197
                                {
198
                                    driveEntryList.Remove(entry);
199
                                    // Log the removal of the drive
200
                                    using (StreamWriter sw = File.AppendText(logLocation))
201
                                    {
202
                                        sw.WriteLine("Drive Removed --  [" + entry.time.ToString() + "]\t" + entry.drive + "\t\"" + entry.label + "\"\t" + entry.size);
203
                                    }
204
                                    break;
205
                                }
206
                            }
207
                            paintDriveListbox();
208
                        }
209
                    }
210
                    // Clears and refreshes the drivesPreviouslyDetected list
48 Kevin 211
                    drivesPreviouslyDetectedList.Clear();
46 Kevin 212
                    foreach (DriveInfo drive in newlyDetectedDrives)
213
                    {
48 Kevin 214
                        drivesPreviouslyDetectedList.Add(drive.Name);
46 Kevin 215
                    }
216
                }
48 Kevin 217
 
46 Kevin 218
                // Sleeps the thread for a specified amount of time
219
                Thread.Sleep(Settings_Form.driveLoggerRefreshInterval);
220
            }
221
        }
222
        void DriveLogger_Form_KeyDown(object sender, KeyEventArgs e)
223
        {
224
            // Monitors for keypresses for the about box and refresh
225
            switch (e.KeyCode)
226
            {
227
                case Keys.OemQuestion:
63 Kevin 228
                    About_Box_Form window = new About_Box_Form();
46 Kevin 229
                    window.ShowDialog();
230
                    break;
231
                case Keys.F5:
232
                    refreshDrives();
233
                    break;
234
            }
235
        }
236
        void listView_Drives_AfterLabelEdit(object sender, LabelEditEventArgs e)
237
        {
238
            // Logs the new label if it is changed
239
            if (e.Label != null)
240
            {
241
                using (StreamWriter sw = File.AppendText(logLocation))
242
                {
243
                    ListViewItem entry = listView_Drives.Items[e.Item];
244
 
245
                    sw.WriteLine("Label \"" + e.Label + "\" added to drive " + entry.SubItems[3].Text);
246
                }
247
            }
248
        }
249
        void DriveLogger_Form_FormClosed(object sender, FormClosedEventArgs e)
250
        {
251
            // Resets variable so form can be opened again
252
            instanceAlreadyRunning = false;
253
        }
254
        private void paintDriveListbox()
255
        {
256
            // Updates the listview
257
            this.listView_Drives.BeginUpdate();
258
 
259
            this.listView_Drives.Items.Clear();
260
 
261
            // Adds each entry from the driveList into the listView
262
            foreach (DriveEntry entry in driveEntryList)
263
            {
264
                if (entry.owner != "System Reserved")
265
                {
266
                    ListViewItem item = new ListViewItem();
267
                    item.Text = entry.owner;
268
                    ListViewItem.ListViewSubItem subDock = new ListViewItem.ListViewSubItem();
269
                    subDock.Text = entry.dock;
270
                    item.SubItems.Add(subDock);
271
                    ListViewItem.ListViewSubItem subTime = new ListViewItem.ListViewSubItem();
272
                    subTime.Text = entry.time.ToString();
273
                    item.SubItems.Add(subTime);
274
                    ListViewItem.ListViewSubItem subDrive = new ListViewItem.ListViewSubItem();
275
                    subDrive.Text = entry.drive;
276
                    item.SubItems.Add(subDrive);
277
                    ListViewItem.ListViewSubItem subLabel = new ListViewItem.ListViewSubItem();
278
                    subLabel.Text = entry.label;
279
                    item.SubItems.Add(subLabel);
280
                    ListViewItem.ListViewSubItem subSize = new ListViewItem.ListViewSubItem();
281
                    subSize.Text = entry.size;
282
                    item.SubItems.Add(subSize);
283
 
284
                    this.listView_Drives.Items.Add(item);
285
                }
286
            }
287
 
288
            this.listView_Drives.EndUpdate();
289
        }
290
        private void refreshDrives()
291
        {
292
            // Checks list of drive entries to see if any drives have been removed
293
            bool removedDriveFound;
294
            List<DriveEntry> drivesToRemove = new List<DriveEntry>();
295
            // Checks each entry in driveList to see if matching drive is found on list
296
            // of current drives. Removes entry from driveList if drive is not found.
297
            DriveInfo[] currentDrives = DriveInfo.GetDrives();
298
            for (int i = 0; i < driveEntryList.Count; i++)
299
            {
300
                DriveEntry storedDrive = driveEntryList[i];
301
                removedDriveFound = false;
302
                // Loop here checks for non-matching entries in the two lists
303
                foreach (DriveInfo currentDrive in currentDrives)
304
                {
305
                    // If entries match, drive was not removed
306
                    if (storedDrive.drive == currentDrive.Name)
307
                    {
308
                        removedDriveFound = true;
309
                        break;
310
                    }
311
                }
312
                // If list mismatch is detected, remove from driveList
313
                if (removedDriveFound == false)
314
                {
315
                    drivesToRemove.Add(storedDrive);
316
                }
317
            }
318
            // Removes drive from driveList
319
            foreach (DriveEntry entry in drivesToRemove)
320
            {
321
                // Removes drive from driveList
322
                foreach (DriveEntry entry2 in driveEntryList)
323
                {
324
                    if (entry.drive == entry2.drive)
325
                    {
326
                        driveEntryList.Remove(entry);
327
                        break;
328
                    }
329
                }
330
            }
331
 
332
            paintDriveListbox();
333
        }
334
    }
335
}