| 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>();
|
| 38 |
Kevin |
30 |
// Log location for the debug and logging texts
|
| 30 |
Kevin |
31 |
private static string logLocation = "C:\\DriveLog.txt";
|
| 38 |
Kevin |
32 |
private static int deviceRemovalRefreshInterval = 1000;
|
|
|
33 |
|
| 28 |
Kevin |
34 |
public MainForm()
|
|
|
35 |
{
|
| 38 |
Kevin |
36 |
// Disable thread safe checking.
|
|
|
37 |
// !! -- NEED TO IMPLEMENT THREAD SAFE UPDATING OF FORM -- !!
|
|
|
38 |
CheckForIllegalCrossThreadCalls = false;
|
|
|
39 |
|
| 28 |
Kevin |
40 |
// Clears the list of DriveEntry structs
|
|
|
41 |
driveList.Clear();
|
|
|
42 |
InitializeComponent();
|
| 38 |
Kevin |
43 |
// Initializes a new DriveDetector class used to detect new drive events
|
| 28 |
Kevin |
44 |
DriveDetector driveDetector = new DriveDetector();
|
|
|
45 |
driveDetector.DeviceArrived += new DriveDetectorEventHandler(driveDetector_DeviceArrived);
|
| 38 |
Kevin |
46 |
//driveDetector.DeviceRemoved += new DriveDetectorEventHandler(driveDetector_DeviceRemoved);
|
| 30 |
Kevin |
47 |
this.listView_Drives.AfterLabelEdit += new LabelEditEventHandler(listView_Drives_AfterLabelEdit);
|
| 28 |
Kevin |
48 |
|
| 38 |
Kevin |
49 |
// KeyPreview events for keyboard shortcuts
|
| 28 |
Kevin |
50 |
this.KeyPreview = true;
|
|
|
51 |
this.KeyPress += new KeyPressEventHandler(MainForm_KeyPress);
|
|
|
52 |
|
| 38 |
Kevin |
53 |
// Appends new session start text to log file
|
| 30 |
Kevin |
54 |
using (StreamWriter sw = File.AppendText(logLocation))
|
|
|
55 |
{
|
|
|
56 |
sw.WriteLine("-- New Session Started --");
|
|
|
57 |
sw.WriteLine("Initializing form details");
|
|
|
58 |
}
|
| 38 |
Kevin |
59 |
|
|
|
60 |
// Adds columns to the listview
|
| 34 |
Kevin |
61 |
this.listView_Drives.Columns.Add("Owner", 145, HorizontalAlignment.Left);
|
|
|
62 |
this.listView_Drives.Columns.Add("Time", 130, HorizontalAlignment.Center);
|
| 28 |
Kevin |
63 |
this.listView_Drives.Columns.Add("Drive", 40, HorizontalAlignment.Center);
|
|
|
64 |
this.listView_Drives.Columns.Add("Label", 109, HorizontalAlignment.Center);
|
|
|
65 |
this.listView_Drives.Columns.Add("Size", 60, HorizontalAlignment.Center);
|
|
|
66 |
|
|
|
67 |
paintDriveListbox();
|
| 38 |
Kevin |
68 |
|
|
|
69 |
// Creates and runs a background worker for detecting if drives have been
|
|
|
70 |
// removed and refreshing the listview accordingly
|
|
|
71 |
BackgroundWorker bgWorker = new BackgroundWorker();
|
|
|
72 |
bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
|
|
|
73 |
|
|
|
74 |
// Starts the background worker thread
|
|
|
75 |
bgWorker.RunWorkerAsync();
|
| 28 |
Kevin |
76 |
}
|
| 38 |
Kevin |
77 |
void bgWorker_DoWork(object sender, DoWorkEventArgs e)
|
|
|
78 |
{
|
|
|
79 |
// currentDrives holds a list of 'previous' drives to compare to
|
|
|
80 |
List<string> currentDrives = new List<string>();
|
|
|
81 |
// newDrives holds a list of the most current drives detected
|
|
|
82 |
DriveInfo[] newDrives = DriveInfo.GetDrives();
|
|
|
83 |
// Sets the two lists of drives to hold the same drives
|
|
|
84 |
foreach (DriveInfo drive in newDrives)
|
|
|
85 |
{
|
|
|
86 |
currentDrives.Add(drive.Name);
|
|
|
87 |
}
|
|
|
88 |
// Loop that checks for drives that are removed
|
|
|
89 |
while (true)
|
|
|
90 |
{
|
|
|
91 |
// Updates the list of current drives
|
|
|
92 |
newDrives = DriveInfo.GetDrives();
|
|
|
93 |
// If the count of current drives is more than the count of previous drives
|
|
|
94 |
if (newDrives.Length < currentDrives.Count)
|
|
|
95 |
{
|
|
|
96 |
// Goes through each list and finds the drive that was recently removed
|
|
|
97 |
bool removedDriveFound = false;
|
|
|
98 |
foreach (string str in currentDrives)
|
|
|
99 |
{
|
|
|
100 |
removedDriveFound = false;
|
|
|
101 |
// Loop here checks for non-matching entries in the two lists
|
|
|
102 |
foreach (DriveInfo drive in newDrives)
|
|
|
103 |
{
|
|
|
104 |
// If entries match, drive was not removed
|
|
|
105 |
if (str == drive.Name)
|
|
|
106 |
{
|
|
|
107 |
removedDriveFound = true;
|
|
|
108 |
break;
|
|
|
109 |
}
|
|
|
110 |
}
|
|
|
111 |
// If list mismatch is detected, remove from driveList
|
|
|
112 |
if (removedDriveFound == false)
|
|
|
113 |
{
|
|
|
114 |
driveRemoved(str);
|
|
|
115 |
}
|
|
|
116 |
}
|
|
|
117 |
// Clears and refreshes the currentDrives list
|
|
|
118 |
currentDrives.Clear();
|
|
|
119 |
foreach (DriveInfo drive in newDrives)
|
|
|
120 |
{
|
|
|
121 |
currentDrives.Add(drive.Name);
|
|
|
122 |
}
|
|
|
123 |
}
|
|
|
124 |
else
|
|
|
125 |
{
|
|
|
126 |
// Sets the two lists to hold the same drives
|
|
|
127 |
currentDrives.Clear();
|
|
|
128 |
foreach (DriveInfo drive in newDrives)
|
|
|
129 |
{
|
|
|
130 |
currentDrives.Add(drive.Name);
|
|
|
131 |
}
|
|
|
132 |
}
|
|
|
133 |
// Sleeps the thread for a second
|
|
|
134 |
System.Threading.Thread.Sleep(deviceRemovalRefreshInterval);
|
|
|
135 |
}
|
|
|
136 |
}
|
|
|
137 |
private void driveRemoved(string str)
|
|
|
138 |
{
|
|
|
139 |
// Removes the passed drive from driveList
|
|
|
140 |
foreach (DriveEntry entry in driveList)
|
|
|
141 |
{
|
|
|
142 |
if (str == entry.drive)
|
|
|
143 |
{
|
|
|
144 |
driveList.Remove(entry);
|
|
|
145 |
using (StreamWriter sw = File.AppendText(logLocation))
|
|
|
146 |
{
|
|
|
147 |
sw.WriteLine("Drive Removed -- [" + entry.time.ToString() + "]\t" + entry.drive + "\t\"" + entry.label + "\"\t" + entry.size);
|
|
|
148 |
}
|
|
|
149 |
break;
|
|
|
150 |
}
|
|
|
151 |
}
|
| 28 |
Kevin |
152 |
|
| 34 |
Kevin |
153 |
paintDriveListbox();
|
|
|
154 |
}
|
| 30 |
Kevin |
155 |
void listView_Drives_AfterLabelEdit(object sender, LabelEditEventArgs e)
|
|
|
156 |
{
|
| 38 |
Kevin |
157 |
// Logs the new label if it is changed
|
| 30 |
Kevin |
158 |
if (e.Label != null)
|
|
|
159 |
{
|
|
|
160 |
using (StreamWriter sw = File.AppendText(logLocation))
|
|
|
161 |
{
|
|
|
162 |
ListViewItem entry = listView_Drives.Items[e.Item];
|
|
|
163 |
|
|
|
164 |
sw.WriteLine("Label \"" + e.Label + "\" added to drive " + entry.SubItems[2].Text);
|
|
|
165 |
}
|
|
|
166 |
}
|
|
|
167 |
}
|
| 28 |
Kevin |
168 |
void MainForm_KeyPress(object sender, KeyPressEventArgs e)
|
|
|
169 |
{
|
|
|
170 |
switch (e.KeyChar)
|
|
|
171 |
{
|
|
|
172 |
case '?':
|
|
|
173 |
AboutBox window = new AboutBox();
|
|
|
174 |
window.ShowDialog();
|
|
|
175 |
break;
|
|
|
176 |
}
|
|
|
177 |
}
|
|
|
178 |
private void paintDriveListbox()
|
|
|
179 |
{
|
| 38 |
Kevin |
180 |
// Updates the listview
|
| 28 |
Kevin |
181 |
this.listView_Drives.BeginUpdate();
|
|
|
182 |
|
|
|
183 |
this.listView_Drives.Items.Clear();
|
|
|
184 |
|
|
|
185 |
// Adds each entry from the driveList into the listView
|
|
|
186 |
foreach (DriveEntry entry in driveList)
|
|
|
187 |
{
|
| 38 |
Kevin |
188 |
if (entry.owner != "System Reserved")
|
|
|
189 |
{
|
|
|
190 |
ListViewItem item = new ListViewItem();
|
|
|
191 |
item.Text = entry.owner;
|
|
|
192 |
ListViewItem.ListViewSubItem subTime = new ListViewItem.ListViewSubItem();
|
|
|
193 |
subTime.Text = entry.time.ToString();
|
|
|
194 |
item.SubItems.Add(subTime);
|
|
|
195 |
ListViewItem.ListViewSubItem subDrive = new ListViewItem.ListViewSubItem();
|
|
|
196 |
subDrive.Text = entry.drive;
|
|
|
197 |
item.SubItems.Add(subDrive);
|
|
|
198 |
ListViewItem.ListViewSubItem subLabel = new ListViewItem.ListViewSubItem();
|
|
|
199 |
subLabel.Text = entry.label;
|
|
|
200 |
item.SubItems.Add(subLabel);
|
|
|
201 |
ListViewItem.ListViewSubItem subSize = new ListViewItem.ListViewSubItem();
|
|
|
202 |
subSize.Text = entry.size;
|
|
|
203 |
item.SubItems.Add(subSize);
|
| 28 |
Kevin |
204 |
|
| 38 |
Kevin |
205 |
this.listView_Drives.Items.Add(item);
|
|
|
206 |
}
|
| 28 |
Kevin |
207 |
}
|
|
|
208 |
|
|
|
209 |
this.listView_Drives.EndUpdate();
|
|
|
210 |
}
|
|
|
211 |
void driveDetector_DeviceArrived(object sender, DriveDetectorEventArgs e)
|
|
|
212 |
{
|
| 38 |
Kevin |
213 |
// Event call for when a new drive is detected by DriveDetector
|
| 28 |
Kevin |
214 |
|
| 38 |
Kevin |
215 |
// Disable e.HookQueryRemoved to prevent a hook being attached to the volume
|
|
|
216 |
//e.HookQueryRemove = true;
|
|
|
217 |
|
|
|
218 |
// Creates and populates a new DriveEntry
|
| 28 |
Kevin |
219 |
DriveEntry newEntry = new DriveEntry();
|
|
|
220 |
newEntry.time = DateTime.Now;
|
|
|
221 |
newEntry.drive = e.Drive;
|
|
|
222 |
|
|
|
223 |
DriveInfo tempDrive = null;
|
|
|
224 |
DriveInfo[] allDrives = DriveInfo.GetDrives();
|
|
|
225 |
foreach (DriveInfo drive in allDrives)
|
|
|
226 |
{
|
|
|
227 |
if (drive.IsReady)
|
|
|
228 |
{
|
|
|
229 |
if (drive.Name == newEntry.drive)
|
|
|
230 |
{
|
|
|
231 |
tempDrive = drive;
|
|
|
232 |
break;
|
|
|
233 |
}
|
|
|
234 |
}
|
|
|
235 |
}
|
|
|
236 |
newEntry.label = tempDrive.VolumeLabel;
|
|
|
237 |
newEntry.size = (tempDrive.TotalSize / 1073741824).ToString() + " GB";
|
|
|
238 |
|
| 38 |
Kevin |
239 |
// Pops up a dialog asking for a new label unless the partition is a system partition
|
| 34 |
Kevin |
240 |
if (newEntry.label != "System Reserved")
|
|
|
241 |
{
|
|
|
242 |
LabelPrompt label = new LabelPrompt();
|
|
|
243 |
label.ShowDialog();
|
|
|
244 |
newEntry.owner = label.driveLabel;
|
|
|
245 |
}
|
| 38 |
Kevin |
246 |
else
|
|
|
247 |
newEntry.owner = "System Reserved";
|
| 34 |
Kevin |
248 |
|
| 38 |
Kevin |
249 |
// Adds the new DriveEntry into driveList
|
|
|
250 |
driveList.Add(newEntry);
|
|
|
251 |
|
| 30 |
Kevin |
252 |
using (StreamWriter sw = File.AppendText(logLocation))
|
|
|
253 |
{
|
| 34 |
Kevin |
254 |
sw.WriteLine("Drive Attached -- [" + newEntry.time.ToString() + "]\t\"" + newEntry.owner + "\"\t" + newEntry.drive + "\t\"" + newEntry.label + "\"\t" + newEntry.size);
|
| 30 |
Kevin |
255 |
}
|
|
|
256 |
|
| 28 |
Kevin |
257 |
paintDriveListbox();
|
|
|
258 |
}
|
| 38 |
Kevin |
259 |
//void driveDetector_DeviceRemoved(object sender, DriveDetectorEventArgs e)
|
|
|
260 |
//{
|
|
|
261 |
// //DriveEntry entryToRemove = new DriveEntry();
|
|
|
262 |
// foreach (DriveEntry entry in driveList)
|
|
|
263 |
// {
|
|
|
264 |
// if (e.Drive == entry.drive)
|
|
|
265 |
// {
|
|
|
266 |
// //entryToRemove = entry;
|
|
|
267 |
// driveList.Remove(entry);
|
|
|
268 |
// break;
|
|
|
269 |
// }
|
|
|
270 |
// }
|
|
|
271 |
// //driveList.Remove(entryToRemove);
|
| 28 |
Kevin |
272 |
|
| 38 |
Kevin |
273 |
// using (StreamWriter sw = File.AppendText(logLocation))
|
|
|
274 |
// {
|
|
|
275 |
// //sw.WriteLine("Drive Removed -- [" + entryToRemove.time.ToString() + "]\t" + entryToRemove.drive + "\t\"" + entryToRemove.label + "\"\t" + entryToRemove.size);
|
|
|
276 |
// }
|
| 30 |
Kevin |
277 |
|
| 38 |
Kevin |
278 |
// paintDriveListbox();
|
|
|
279 |
//}
|
| 28 |
Kevin |
280 |
}
|
|
|
281 |
}
|