| 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;
|
|
|
10 |
using Dolinay; // Imported class for drive insertion event handling
|
|
|
11 |
|
|
|
12 |
namespace DriveLogger
|
|
|
13 |
{
|
|
|
14 |
public struct DriveEntry
|
|
|
15 |
{
|
|
|
16 |
/// <summary>
|
|
|
17 |
/// Struct for storing the info on each drive that is inserted
|
|
|
18 |
/// </summary>
|
|
|
19 |
|
|
|
20 |
public DateTime time;
|
|
|
21 |
public string status;
|
|
|
22 |
public string drive;
|
|
|
23 |
public string label;
|
|
|
24 |
public string owner;
|
|
|
25 |
public string size;
|
|
|
26 |
}
|
|
|
27 |
public partial class MainForm : Form
|
|
|
28 |
{
|
|
|
29 |
private static List<DriveEntry> driveList = new List<DriveEntry>();
|
|
|
30 |
|
|
|
31 |
public MainForm()
|
|
|
32 |
{
|
|
|
33 |
// Clears the list of DriveEntry structs
|
|
|
34 |
driveList.Clear();
|
|
|
35 |
InitializeComponent();
|
|
|
36 |
DriveDetector driveDetector = new DriveDetector();
|
|
|
37 |
driveDetector.DeviceArrived += new DriveDetectorEventHandler(driveDetector_DeviceArrived);
|
|
|
38 |
driveDetector.DeviceRemoved += new DriveDetectorEventHandler(driveDetector_DeviceRemoved);
|
|
|
39 |
|
|
|
40 |
this.KeyPreview = true;
|
|
|
41 |
this.KeyPress += new KeyPressEventHandler(MainForm_KeyPress);
|
|
|
42 |
|
|
|
43 |
// Adds columns to the listview in the form
|
|
|
44 |
//this.listView_Drives.Columns.Add("Event", 60, HorizontalAlignment.Center);
|
|
|
45 |
this.listView_Drives.Columns.Add("Time", 125, HorizontalAlignment.Center);
|
|
|
46 |
this.listView_Drives.Columns.Add("Drive", 40, HorizontalAlignment.Center);
|
|
|
47 |
this.listView_Drives.Columns.Add("Label", 109, HorizontalAlignment.Center);
|
|
|
48 |
this.listView_Drives.Columns.Add("Size", 60, HorizontalAlignment.Center);
|
|
|
49 |
//this.listView_Drives.Columns.Add("Owner", 95, HorizontalAlignment.Center);
|
|
|
50 |
|
|
|
51 |
paintDriveListbox();
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
void MainForm_KeyPress(object sender, KeyPressEventArgs e)
|
|
|
55 |
{
|
|
|
56 |
switch (e.KeyChar)
|
|
|
57 |
{
|
|
|
58 |
case '?':
|
|
|
59 |
AboutBox window = new AboutBox();
|
|
|
60 |
window.ShowDialog();
|
|
|
61 |
break;
|
|
|
62 |
}
|
|
|
63 |
}
|
|
|
64 |
private void paintDriveListbox()
|
|
|
65 |
{
|
|
|
66 |
this.listView_Drives.BeginUpdate();
|
|
|
67 |
|
|
|
68 |
this.listView_Drives.Items.Clear();
|
|
|
69 |
|
|
|
70 |
// Adds each entry from the driveList into the listView
|
|
|
71 |
foreach (DriveEntry entry in driveList)
|
|
|
72 |
{
|
|
|
73 |
ListViewItem item = new ListViewItem();
|
|
|
74 |
//item.Text = entry.status;
|
|
|
75 |
item.Text = entry.time.ToString();
|
|
|
76 |
//ListViewItem.ListViewSubItem subTime = new ListViewItem.ListViewSubItem();
|
|
|
77 |
//subTime.Text = entry.time.ToString();
|
|
|
78 |
//item.SubItems.Add(subTime);
|
|
|
79 |
ListViewItem.ListViewSubItem subDrive = new ListViewItem.ListViewSubItem();
|
|
|
80 |
subDrive.Text = entry.drive;
|
|
|
81 |
item.SubItems.Add(subDrive);
|
|
|
82 |
ListViewItem.ListViewSubItem subLabel = new ListViewItem.ListViewSubItem();
|
|
|
83 |
subLabel.Text = entry.label;
|
|
|
84 |
item.SubItems.Add(subLabel);
|
|
|
85 |
ListViewItem.ListViewSubItem subSize = new ListViewItem.ListViewSubItem();
|
|
|
86 |
subSize.Text = entry.size;
|
|
|
87 |
item.SubItems.Add(subSize);
|
|
|
88 |
ListViewItem.ListViewSubItem subOwner = new ListViewItem.ListViewSubItem();
|
|
|
89 |
subOwner.Text = entry.owner;
|
|
|
90 |
item.SubItems.Add(subOwner);
|
|
|
91 |
|
|
|
92 |
this.listView_Drives.Items.Add(item);
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
this.listView_Drives.EndUpdate();
|
|
|
96 |
}
|
|
|
97 |
void driveDetector_DeviceArrived(object sender, DriveDetectorEventArgs e)
|
|
|
98 |
{
|
|
|
99 |
e.HookQueryRemove = true;
|
|
|
100 |
|
|
|
101 |
DriveEntry newEntry = new DriveEntry();
|
|
|
102 |
newEntry.status = "Inserted";
|
|
|
103 |
newEntry.time = DateTime.Now;
|
|
|
104 |
newEntry.drive = e.Drive;
|
|
|
105 |
|
|
|
106 |
DriveInfo tempDrive = null;
|
|
|
107 |
DriveInfo[] allDrives = DriveInfo.GetDrives();
|
|
|
108 |
foreach (DriveInfo drive in allDrives)
|
|
|
109 |
{
|
|
|
110 |
if (drive.IsReady)
|
|
|
111 |
{
|
|
|
112 |
if (drive.Name == newEntry.drive)
|
|
|
113 |
{
|
|
|
114 |
tempDrive = drive;
|
|
|
115 |
break;
|
|
|
116 |
}
|
|
|
117 |
}
|
|
|
118 |
}
|
|
|
119 |
newEntry.label = tempDrive.VolumeLabel;
|
|
|
120 |
newEntry.size = (tempDrive.TotalSize / 1073741824).ToString() + " GB";
|
|
|
121 |
newEntry.owner = "";
|
|
|
122 |
driveList.Add(newEntry);
|
|
|
123 |
|
|
|
124 |
paintDriveListbox();
|
|
|
125 |
}
|
|
|
126 |
void driveDetector_DeviceRemoved(object sender, DriveDetectorEventArgs e)
|
|
|
127 |
{
|
|
|
128 |
DriveEntry entryToRemove = new DriveEntry();
|
|
|
129 |
foreach (DriveEntry entry in driveList)
|
|
|
130 |
{
|
|
|
131 |
if (e.Drive == entry.drive)
|
|
|
132 |
{
|
|
|
133 |
entryToRemove = entry;
|
|
|
134 |
break;
|
|
|
135 |
}
|
|
|
136 |
}
|
|
|
137 |
driveList.Remove(entryToRemove);
|
|
|
138 |
|
|
|
139 |
paintDriveListbox();
|
|
|
140 |
}
|
|
|
141 |
}
|
|
|
142 |
}
|