| 28 |
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;
|
| 38 |
Kevin |
10 |
// Imported class for drive insertion event handling
|
|
|
11 |
using Dolinay;
|
| 28 |
Kevin |
12 |
|
|
|
13 |
namespace DriveLogger
|
|
|
14 |
{
|
| 38 |
Kevin |
15 |
/// <summary>
|
|
|
16 |
/// Struct used for storing detailed information for each drive that is detected
|
|
|
17 |
/// </summary>
|
| 28 |
Kevin |
18 |
public struct DriveEntry
|
|
|
19 |
{
|
|
|
20 |
public DateTime time;
|
|
|
21 |
public string drive;
|
|
|
22 |
public string label;
|
|
|
23 |
public string size;
|
| 34 |
Kevin |
24 |
public string owner;
|
| 28 |
Kevin |
25 |
}
|
|
|
26 |
public partial class MainForm : Form
|
|
|
27 |
{
|
| 38 |
Kevin |
28 |
// List of drives that are displayed in the listview
|
| 28 |
Kevin |
29 |
private static List<DriveEntry> driveList = new List<DriveEntry>();
|
| 40 |
Kevin |
30 |
private static DateTime lastDriveInsertedTime;
|
|
|
31 |
// Comparison time is set here for between drive detections
|
|
|
32 |
// Format is in hours, minutes, seconds
|
|
|
33 |
TimeSpan detectionTime = new TimeSpan(0, 0, 0, 10, 0);
|
| 38 |
Kevin |
34 |
// Log location for the debug and logging texts
|
| 30 |
Kevin |
35 |
private static string logLocation = "C:\\DriveLog.txt";
|
| 38 |
Kevin |
36 |
private static int deviceRemovalRefreshInterval = 1000;
|
|
|
37 |
|
| 28 |
Kevin |
38 |
public MainForm()
|
|
|
39 |
{
|
| 38 |
Kevin |
40 |
// Disable thread safe checking.
|
|
|
41 |
// !! -- NEED TO IMPLEMENT THREAD SAFE UPDATING OF FORM -- !!
|
|
|
42 |
CheckForIllegalCrossThreadCalls = false;
|
|
|
43 |
|
| 28 |
Kevin |
44 |
// Clears the list of DriveEntry structs
|
|
|
45 |
driveList.Clear();
|
|
|
46 |
InitializeComponent();
|
| 38 |
Kevin |
47 |
// Initializes a new DriveDetector class used to detect new drive events
|
| 28 |
Kevin |
48 |
DriveDetector driveDetector = new DriveDetector();
|
|
|
49 |
driveDetector.DeviceArrived += new DriveDetectorEventHandler(driveDetector_DeviceArrived);
|
| 38 |
Kevin |
50 |
//driveDetector.DeviceRemoved += new DriveDetectorEventHandler(driveDetector_DeviceRemoved);
|
| 30 |
Kevin |
51 |
this.listView_Drives.AfterLabelEdit += new LabelEditEventHandler(listView_Drives_AfterLabelEdit);
|
| 28 |
Kevin |
52 |
|
| 38 |
Kevin |
53 |
// KeyPreview events for keyboard shortcuts
|
| 28 |
Kevin |
54 |
this.KeyPreview = true;
|
| 40 |
Kevin |
55 |
this.KeyDown += new KeyEventHandler(MainForm_KeyDown);
|
| 28 |
Kevin |
56 |
|
| 38 |
Kevin |
57 |
// Appends new session start text to log file
|
| 30 |
Kevin |
58 |
using (StreamWriter sw = File.AppendText(logLocation))
|
|
|
59 |
{
|
|
|
60 |
sw.WriteLine("-- New Session Started --");
|
|
|
61 |
sw.WriteLine("Initializing form details");
|
|
|
62 |
}
|
| 38 |
Kevin |
63 |
|
|
|
64 |
// Adds columns to the listview
|
| 34 |
Kevin |
65 |
this.listView_Drives.Columns.Add("Owner", 145, HorizontalAlignment.Left);
|
|
|
66 |
this.listView_Drives.Columns.Add("Time", 130, HorizontalAlignment.Center);
|
| 28 |
Kevin |
67 |
this.listView_Drives.Columns.Add("Drive", 40, HorizontalAlignment.Center);
|
|
|
68 |
this.listView_Drives.Columns.Add("Label", 109, HorizontalAlignment.Center);
|
|
|
69 |
this.listView_Drives.Columns.Add("Size", 60, HorizontalAlignment.Center);
|
|
|
70 |
|
|
|
71 |
paintDriveListbox();
|
| 38 |
Kevin |
72 |
|
|
|
73 |
// Creates and runs a background worker for detecting if drives have been
|
|
|
74 |
// removed and refreshing the listview accordingly
|
|
|
75 |
BackgroundWorker bgWorker = new BackgroundWorker();
|
|
|
76 |
bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
|
|
|
77 |
|
|
|
78 |
// Starts the background worker thread
|
|
|
79 |
bgWorker.RunWorkerAsync();
|
| 40 |
Kevin |
80 |
}
|
|
|
81 |
void MainForm_KeyDown(object sender, KeyEventArgs e)
|
|
|
82 |
{
|
|
|
83 |
switch (e.KeyCode)
|
|
|
84 |
{
|
|
|
85 |
case Keys.OemQuestion:
|
|
|
86 |
AboutBox window = new AboutBox();
|
|
|
87 |
window.ShowDialog();
|
|
|
88 |
break;
|
|
|
89 |
case Keys.F5:
|
|
|
90 |
refreshDrives();
|
|
|
91 |
break;
|
|
|
92 |
}
|
|
|
93 |
|
| 28 |
Kevin |
94 |
}
|
| 38 |
Kevin |
95 |
void bgWorker_DoWork(object sender, DoWorkEventArgs e)
|
|
|
96 |
{
|
|
|
97 |
// currentDrives holds a list of 'previous' drives to compare to
|
|
|
98 |
List<string> currentDrives = new List<string>();
|
|
|
99 |
// newDrives holds a list of the most current drives detected
|
|
|
100 |
DriveInfo[] newDrives = DriveInfo.GetDrives();
|
|
|
101 |
// Sets the two lists of drives to hold the same drives
|
|
|
102 |
foreach (DriveInfo drive in newDrives)
|
|
|
103 |
{
|
|
|
104 |
currentDrives.Add(drive.Name);
|
|
|
105 |
}
|
|
|
106 |
// Loop that checks for drives that are removed
|
|
|
107 |
while (true)
|
|
|
108 |
{
|
|
|
109 |
// Updates the list of current drives
|
|
|
110 |
newDrives = DriveInfo.GetDrives();
|
|
|
111 |
// If the count of current drives is more than the count of previous drives
|
|
|
112 |
if (newDrives.Length < currentDrives.Count)
|
|
|
113 |
{
|
|
|
114 |
// Goes through each list and finds the drive that was recently removed
|
|
|
115 |
bool removedDriveFound = false;
|
|
|
116 |
foreach (string str in currentDrives)
|
|
|
117 |
{
|
|
|
118 |
removedDriveFound = false;
|
|
|
119 |
// Loop here checks for non-matching entries in the two lists
|
|
|
120 |
foreach (DriveInfo drive in newDrives)
|
|
|
121 |
{
|
|
|
122 |
// If entries match, drive was not removed
|
|
|
123 |
if (str == drive.Name)
|
|
|
124 |
{
|
|
|
125 |
removedDriveFound = true;
|
|
|
126 |
break;
|
|
|
127 |
}
|
|
|
128 |
}
|
|
|
129 |
// If list mismatch is detected, remove from driveList
|
|
|
130 |
if (removedDriveFound == false)
|
|
|
131 |
{
|
| 40 |
Kevin |
132 |
// Removes drive from driveList
|
|
|
133 |
foreach (DriveEntry entry in driveList)
|
|
|
134 |
{
|
|
|
135 |
if (str == entry.drive)
|
|
|
136 |
{
|
|
|
137 |
driveList.Remove(entry);
|
|
|
138 |
using (StreamWriter sw = File.AppendText(logLocation))
|
|
|
139 |
{
|
|
|
140 |
sw.WriteLine("Drive Removed -- [" + entry.time.ToString() + "]\t" + entry.drive + "\t\"" + entry.label + "\"\t" + entry.size);
|
|
|
141 |
}
|
|
|
142 |
break;
|
|
|
143 |
}
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
paintDriveListbox();
|
| 38 |
Kevin |
147 |
}
|
|
|
148 |
}
|
|
|
149 |
// Clears and refreshes the currentDrives list
|
|
|
150 |
currentDrives.Clear();
|
|
|
151 |
foreach (DriveInfo drive in newDrives)
|
|
|
152 |
{
|
|
|
153 |
currentDrives.Add(drive.Name);
|
|
|
154 |
}
|
|
|
155 |
}
|
|
|
156 |
else
|
|
|
157 |
{
|
|
|
158 |
// Sets the two lists to hold the same drives
|
|
|
159 |
currentDrives.Clear();
|
|
|
160 |
foreach (DriveInfo drive in newDrives)
|
|
|
161 |
{
|
|
|
162 |
currentDrives.Add(drive.Name);
|
|
|
163 |
}
|
|
|
164 |
}
|
|
|
165 |
// Sleeps the thread for a second
|
|
|
166 |
System.Threading.Thread.Sleep(deviceRemovalRefreshInterval);
|
|
|
167 |
}
|
|
|
168 |
}
|
| 40 |
Kevin |
169 |
void driveDetector_DeviceArrived(object sender, DriveDetectorEventArgs e)
|
| 38 |
Kevin |
170 |
{
|
| 40 |
Kevin |
171 |
// Event call for when a new drive is detected by DriveDetector
|
|
|
172 |
|
|
|
173 |
// Disable e.HookQueryRemoved to prevent a hook being attached to the volume
|
|
|
174 |
//e.HookQueryRemove = true;
|
|
|
175 |
|
|
|
176 |
// Creates and populates a new DriveEntry
|
|
|
177 |
DriveEntry newEntry = new DriveEntry();
|
|
|
178 |
newEntry.time = DateTime.Now;
|
|
|
179 |
newEntry.drive = e.Drive;
|
|
|
180 |
|
|
|
181 |
DriveInfo tempDrive = null;
|
|
|
182 |
DriveInfo[] allDrives = DriveInfo.GetDrives();
|
|
|
183 |
foreach (DriveInfo drive in allDrives)
|
| 38 |
Kevin |
184 |
{
|
| 40 |
Kevin |
185 |
if (drive.IsReady)
|
| 38 |
Kevin |
186 |
{
|
| 40 |
Kevin |
187 |
if (drive.Name == newEntry.drive)
|
| 38 |
Kevin |
188 |
{
|
| 40 |
Kevin |
189 |
tempDrive = drive;
|
|
|
190 |
break;
|
| 38 |
Kevin |
191 |
}
|
|
|
192 |
}
|
|
|
193 |
}
|
| 40 |
Kevin |
194 |
newEntry.label = tempDrive.VolumeLabel;
|
| 28 |
Kevin |
195 |
|
| 40 |
Kevin |
196 |
// Determines the size of the attached drive
|
|
|
197 |
if ((tempDrive.TotalSize / 1073741824) > 0)
|
|
|
198 |
newEntry.size = (tempDrive.TotalSize / 1073741824).ToString() + " GB";
|
|
|
199 |
else
|
|
|
200 |
newEntry.size = (tempDrive.TotalSize / 1048576).ToString() + " MB";
|
|
|
201 |
|
|
|
202 |
// Checks if the drive was detected within a second of the previous drive
|
|
|
203 |
// If so, set the drive label to point to the previous drive
|
|
|
204 |
int compare = (newEntry.time.Subtract(lastDriveInsertedTime)).CompareTo(detectionTime);
|
|
|
205 |
// Sets the last time to be the time of the recently detected drive
|
|
|
206 |
lastDriveInsertedTime = newEntry.time;
|
|
|
207 |
if (compare <= 0)
|
|
|
208 |
{
|
|
|
209 |
newEntry.owner = " ^ -- Same As -- ^ ";
|
|
|
210 |
lastDriveInsertedTime = newEntry.time;
|
|
|
211 |
}
|
|
|
212 |
else
|
|
|
213 |
{
|
|
|
214 |
LabelPrompt label = new LabelPrompt();
|
|
|
215 |
label.ShowDialog();
|
|
|
216 |
newEntry.owner = label.driveLabel;
|
|
|
217 |
}
|
|
|
218 |
|
|
|
219 |
// Adds the new DriveEntry into driveList
|
|
|
220 |
driveList.Add(newEntry);
|
|
|
221 |
|
|
|
222 |
using (StreamWriter sw = File.AppendText(logLocation))
|
|
|
223 |
{
|
|
|
224 |
sw.WriteLine("Drive Attached -- [" + newEntry.time.ToString() + "]\t\"" + newEntry.owner + "\"\t" + newEntry.drive + "\t\"" + newEntry.label + "\"\t" + newEntry.size);
|
|
|
225 |
}
|
|
|
226 |
|
| 34 |
Kevin |
227 |
paintDriveListbox();
|
|
|
228 |
}
|
| 30 |
Kevin |
229 |
void listView_Drives_AfterLabelEdit(object sender, LabelEditEventArgs e)
|
|
|
230 |
{
|
| 38 |
Kevin |
231 |
// Logs the new label if it is changed
|
| 30 |
Kevin |
232 |
if (e.Label != null)
|
|
|
233 |
{
|
|
|
234 |
using (StreamWriter sw = File.AppendText(logLocation))
|
|
|
235 |
{
|
|
|
236 |
ListViewItem entry = listView_Drives.Items[e.Item];
|
|
|
237 |
|
|
|
238 |
sw.WriteLine("Label \"" + e.Label + "\" added to drive " + entry.SubItems[2].Text);
|
|
|
239 |
}
|
|
|
240 |
}
|
|
|
241 |
}
|
| 28 |
Kevin |
242 |
private void paintDriveListbox()
|
|
|
243 |
{
|
| 38 |
Kevin |
244 |
// Updates the listview
|
| 28 |
Kevin |
245 |
this.listView_Drives.BeginUpdate();
|
|
|
246 |
|
|
|
247 |
this.listView_Drives.Items.Clear();
|
|
|
248 |
|
|
|
249 |
// Adds each entry from the driveList into the listView
|
|
|
250 |
foreach (DriveEntry entry in driveList)
|
|
|
251 |
{
|
| 38 |
Kevin |
252 |
if (entry.owner != "System Reserved")
|
|
|
253 |
{
|
|
|
254 |
ListViewItem item = new ListViewItem();
|
|
|
255 |
item.Text = entry.owner;
|
|
|
256 |
ListViewItem.ListViewSubItem subTime = new ListViewItem.ListViewSubItem();
|
|
|
257 |
subTime.Text = entry.time.ToString();
|
|
|
258 |
item.SubItems.Add(subTime);
|
|
|
259 |
ListViewItem.ListViewSubItem subDrive = new ListViewItem.ListViewSubItem();
|
|
|
260 |
subDrive.Text = entry.drive;
|
|
|
261 |
item.SubItems.Add(subDrive);
|
|
|
262 |
ListViewItem.ListViewSubItem subLabel = new ListViewItem.ListViewSubItem();
|
|
|
263 |
subLabel.Text = entry.label;
|
|
|
264 |
item.SubItems.Add(subLabel);
|
|
|
265 |
ListViewItem.ListViewSubItem subSize = new ListViewItem.ListViewSubItem();
|
|
|
266 |
subSize.Text = entry.size;
|
|
|
267 |
item.SubItems.Add(subSize);
|
| 28 |
Kevin |
268 |
|
| 38 |
Kevin |
269 |
this.listView_Drives.Items.Add(item);
|
|
|
270 |
}
|
| 28 |
Kevin |
271 |
}
|
|
|
272 |
|
|
|
273 |
this.listView_Drives.EndUpdate();
|
|
|
274 |
}
|
| 40 |
Kevin |
275 |
private void refreshDrives()
|
| 28 |
Kevin |
276 |
{
|
| 40 |
Kevin |
277 |
bool removedDriveFound;
|
|
|
278 |
List<DriveEntry> drivesToRemove = new List<DriveEntry>();
|
|
|
279 |
// Checks each entry in driveList to see if matching drive is found on list
|
|
|
280 |
// of current drives. Removes entry from driveList if drive is not found.
|
|
|
281 |
DriveInfo[] currentDrives = DriveInfo.GetDrives();
|
|
|
282 |
for (int i = 0; i < driveList.Count; i++)
|
|
|
283 |
//foreach (DriveEntry storedDrives in driveList)
|
| 28 |
Kevin |
284 |
{
|
| 40 |
Kevin |
285 |
DriveEntry storedDrive = driveList[i];
|
|
|
286 |
removedDriveFound = false;
|
|
|
287 |
// Loop here checks for non-matching entries in the two lists
|
|
|
288 |
foreach (DriveInfo currentDrive in currentDrives)
|
| 28 |
Kevin |
289 |
{
|
| 40 |
Kevin |
290 |
// If entries match, drive was not removed
|
|
|
291 |
if (storedDrive.drive == currentDrive.Name)
|
| 28 |
Kevin |
292 |
{
|
| 40 |
Kevin |
293 |
removedDriveFound = true;
|
| 28 |
Kevin |
294 |
break;
|
|
|
295 |
}
|
|
|
296 |
}
|
| 40 |
Kevin |
297 |
// If list mismatch is detected, remove from driveList
|
|
|
298 |
if (removedDriveFound == false)
|
|
|
299 |
{
|
|
|
300 |
drivesToRemove.Add(storedDrive);
|
|
|
301 |
}
|
| 28 |
Kevin |
302 |
}
|
| 40 |
Kevin |
303 |
// Removes drive from driveList
|
|
|
304 |
foreach (DriveEntry entry in drivesToRemove)
|
| 34 |
Kevin |
305 |
{
|
| 40 |
Kevin |
306 |
// Removes drive from driveList
|
|
|
307 |
foreach (DriveEntry entry2 in driveList)
|
|
|
308 |
{
|
|
|
309 |
if (entry.drive == entry2.drive)
|
|
|
310 |
{
|
|
|
311 |
driveList.Remove(entry);
|
|
|
312 |
break;
|
|
|
313 |
}
|
|
|
314 |
}
|
| 34 |
Kevin |
315 |
}
|
|
|
316 |
|
| 28 |
Kevin |
317 |
paintDriveListbox();
|
|
|
318 |
}
|
| 38 |
Kevin |
319 |
//void driveDetector_DeviceRemoved(object sender, DriveDetectorEventArgs e)
|
|
|
320 |
//{
|
|
|
321 |
// //DriveEntry entryToRemove = new DriveEntry();
|
|
|
322 |
// foreach (DriveEntry entry in driveList)
|
|
|
323 |
// {
|
|
|
324 |
// if (e.Drive == entry.drive)
|
|
|
325 |
// {
|
|
|
326 |
// //entryToRemove = entry;
|
|
|
327 |
// driveList.Remove(entry);
|
|
|
328 |
// break;
|
|
|
329 |
// }
|
|
|
330 |
// }
|
|
|
331 |
// //driveList.Remove(entryToRemove);
|
| 28 |
Kevin |
332 |
|
| 38 |
Kevin |
333 |
// using (StreamWriter sw = File.AppendText(logLocation))
|
|
|
334 |
// {
|
|
|
335 |
// //sw.WriteLine("Drive Removed -- [" + entryToRemove.time.ToString() + "]\t" + entryToRemove.drive + "\t\"" + entryToRemove.label + "\"\t" + entryToRemove.size);
|
|
|
336 |
// }
|
| 30 |
Kevin |
337 |
|
| 38 |
Kevin |
338 |
// paintDriveListbox();
|
|
|
339 |
//}
|
| 28 |
Kevin |
340 |
}
|
|
|
341 |
}
|