27,6 → 27,10 |
{ |
// List of drives that are displayed in the listview |
private static List<DriveEntry> driveList = new List<DriveEntry>(); |
private static DateTime lastDriveInsertedTime; |
// Comparison time is set here for between drive detections |
// Format is in hours, minutes, seconds |
TimeSpan detectionTime = new TimeSpan(0, 0, 0, 10, 0); |
// Log location for the debug and logging texts |
private static string logLocation = "C:\\DriveLog.txt"; |
private static int deviceRemovalRefreshInterval = 1000; |
48,7 → 52,7 |
|
// KeyPreview events for keyboard shortcuts |
this.KeyPreview = true; |
this.KeyPress += new KeyPressEventHandler(MainForm_KeyPress); |
this.KeyDown += new KeyEventHandler(MainForm_KeyDown); |
|
// Appends new session start text to log file |
using (StreamWriter sw = File.AppendText(logLocation)) |
73,6 → 77,20 |
|
// Starts the background worker thread |
bgWorker.RunWorkerAsync(); |
} |
void MainForm_KeyDown(object sender, KeyEventArgs e) |
{ |
switch (e.KeyCode) |
{ |
case Keys.OemQuestion: |
AboutBox window = new AboutBox(); |
window.ShowDialog(); |
break; |
case Keys.F5: |
refreshDrives(); |
break; |
} |
|
} |
void bgWorker_DoWork(object sender, DoWorkEventArgs e) |
{ |
111,7 → 129,21 |
// If list mismatch is detected, remove from driveList |
if (removedDriveFound == false) |
{ |
driveRemoved(str); |
// Removes drive from driveList |
foreach (DriveEntry entry in driveList) |
{ |
if (str == entry.drive) |
{ |
driveList.Remove(entry); |
using (StreamWriter sw = File.AppendText(logLocation)) |
{ |
sw.WriteLine("Drive Removed -- [" + entry.time.ToString() + "]\t" + entry.drive + "\t\"" + entry.label + "\"\t" + entry.size); |
} |
break; |
} |
} |
|
paintDriveListbox(); |
} |
} |
// Clears and refreshes the currentDrives list |
134,22 → 166,64 |
System.Threading.Thread.Sleep(deviceRemovalRefreshInterval); |
} |
} |
private void driveRemoved(string str) |
void driveDetector_DeviceArrived(object sender, DriveDetectorEventArgs e) |
{ |
// Removes the passed drive from driveList |
foreach (DriveEntry entry in driveList) |
// Event call for when a new drive is detected by DriveDetector |
|
// Disable e.HookQueryRemoved to prevent a hook being attached to the volume |
//e.HookQueryRemove = true; |
|
// Creates and populates a new DriveEntry |
DriveEntry newEntry = new DriveEntry(); |
newEntry.time = DateTime.Now; |
newEntry.drive = e.Drive; |
|
DriveInfo tempDrive = null; |
DriveInfo[] allDrives = DriveInfo.GetDrives(); |
foreach (DriveInfo drive in allDrives) |
{ |
if (str == entry.drive) |
if (drive.IsReady) |
{ |
driveList.Remove(entry); |
using (StreamWriter sw = File.AppendText(logLocation)) |
if (drive.Name == newEntry.drive) |
{ |
sw.WriteLine("Drive Removed -- [" + entry.time.ToString() + "]\t" + entry.drive + "\t\"" + entry.label + "\"\t" + entry.size); |
tempDrive = drive; |
break; |
} |
break; |
} |
} |
newEntry.label = tempDrive.VolumeLabel; |
|
// Determines the size of the attached drive |
if ((tempDrive.TotalSize / 1073741824) > 0) |
newEntry.size = (tempDrive.TotalSize / 1073741824).ToString() + " GB"; |
else |
newEntry.size = (tempDrive.TotalSize / 1048576).ToString() + " MB"; |
|
// Checks if the drive was detected within a second of the previous drive |
// If so, set the drive label to point to the previous drive |
int compare = (newEntry.time.Subtract(lastDriveInsertedTime)).CompareTo(detectionTime); |
// Sets the last time to be the time of the recently detected drive |
lastDriveInsertedTime = newEntry.time; |
if (compare <= 0) |
{ |
newEntry.owner = " ^ -- Same As -- ^ "; |
lastDriveInsertedTime = newEntry.time; |
} |
else |
{ |
LabelPrompt label = new LabelPrompt(); |
label.ShowDialog(); |
newEntry.owner = label.driveLabel; |
} |
|
// Adds the new DriveEntry into driveList |
driveList.Add(newEntry); |
|
using (StreamWriter sw = File.AppendText(logLocation)) |
{ |
sw.WriteLine("Drive Attached -- [" + newEntry.time.ToString() + "]\t\"" + newEntry.owner + "\"\t" + newEntry.drive + "\t\"" + newEntry.label + "\"\t" + newEntry.size); |
} |
|
paintDriveListbox(); |
} |
void listView_Drives_AfterLabelEdit(object sender, LabelEditEventArgs e) |
165,16 → 239,6 |
} |
} |
} |
void MainForm_KeyPress(object sender, KeyPressEventArgs e) |
{ |
switch (e.KeyChar) |
{ |
case '?': |
AboutBox window = new AboutBox(); |
window.ShowDialog(); |
break; |
} |
} |
private void paintDriveListbox() |
{ |
// Updates the listview |
208,52 → 272,48 |
|
this.listView_Drives.EndUpdate(); |
} |
void driveDetector_DeviceArrived(object sender, DriveDetectorEventArgs e) |
private void refreshDrives() |
{ |
// Event call for when a new drive is detected by DriveDetector |
|
// Disable e.HookQueryRemoved to prevent a hook being attached to the volume |
//e.HookQueryRemove = true; |
|
// Creates and populates a new DriveEntry |
DriveEntry newEntry = new DriveEntry(); |
newEntry.time = DateTime.Now; |
newEntry.drive = e.Drive; |
|
DriveInfo tempDrive = null; |
DriveInfo[] allDrives = DriveInfo.GetDrives(); |
foreach (DriveInfo drive in allDrives) |
bool removedDriveFound; |
List<DriveEntry> drivesToRemove = new List<DriveEntry>(); |
// Checks each entry in driveList to see if matching drive is found on list |
// of current drives. Removes entry from driveList if drive is not found. |
DriveInfo[] currentDrives = DriveInfo.GetDrives(); |
for (int i = 0; i < driveList.Count; i++) |
//foreach (DriveEntry storedDrives in driveList) |
{ |
if (drive.IsReady) |
DriveEntry storedDrive = driveList[i]; |
removedDriveFound = false; |
// Loop here checks for non-matching entries in the two lists |
foreach (DriveInfo currentDrive in currentDrives) |
{ |
if (drive.Name == newEntry.drive) |
// If entries match, drive was not removed |
if (storedDrive.drive == currentDrive.Name) |
{ |
tempDrive = drive; |
removedDriveFound = true; |
break; |
} |
} |
// If list mismatch is detected, remove from driveList |
if (removedDriveFound == false) |
{ |
drivesToRemove.Add(storedDrive); |
} |
} |
newEntry.label = tempDrive.VolumeLabel; |
newEntry.size = (tempDrive.TotalSize / 1073741824).ToString() + " GB"; |
|
// Pops up a dialog asking for a new label unless the partition is a system partition |
if (newEntry.label != "System Reserved") |
// Removes drive from driveList |
foreach (DriveEntry entry in drivesToRemove) |
{ |
LabelPrompt label = new LabelPrompt(); |
label.ShowDialog(); |
newEntry.owner = label.driveLabel; |
// Removes drive from driveList |
foreach (DriveEntry entry2 in driveList) |
{ |
if (entry.drive == entry2.drive) |
{ |
driveList.Remove(entry); |
break; |
} |
} |
} |
else |
newEntry.owner = "System Reserved"; |
|
// Adds the new DriveEntry into driveList |
driveList.Add(newEntry); |
|
using (StreamWriter sw = File.AppendText(logLocation)) |
{ |
sw.WriteLine("Drive Attached -- [" + newEntry.time.ToString() + "]\t\"" + newEntry.owner + "\"\t" + newEntry.drive + "\t\"" + newEntry.label + "\"\t" + newEntry.size); |
} |
|
paintDriveListbox(); |
} |
//void driveDetector_DeviceRemoved(object sender, DriveDetectorEventArgs e) |