Subversion Repositories Code-Repo

Rev

Rev 38 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 38 Rev 40
Line 25... Line 25...
25
    }
25
    }
26
    public partial class MainForm : Form
26
    public partial class MainForm : Form
27
    {
27
    {
28
        // List of drives that are displayed in the listview
28
        // List of drives that are displayed in the listview
29
        private static List<DriveEntry> driveList = new List<DriveEntry>();
29
        private static List<DriveEntry> driveList = new List<DriveEntry>();
-
 
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);
30
        // Log location for the debug and logging texts
34
        // Log location for the debug and logging texts
31
        private static string logLocation = "C:\\DriveLog.txt";
35
        private static string logLocation = "C:\\DriveLog.txt";
32
        private static int deviceRemovalRefreshInterval = 1000;
36
        private static int deviceRemovalRefreshInterval = 1000;
33
        
37
        
34
        public MainForm()
38
        public MainForm()
Line 46... Line 50...
46
            //driveDetector.DeviceRemoved += new DriveDetectorEventHandler(driveDetector_DeviceRemoved);
50
            //driveDetector.DeviceRemoved += new DriveDetectorEventHandler(driveDetector_DeviceRemoved);
47
            this.listView_Drives.AfterLabelEdit += new LabelEditEventHandler(listView_Drives_AfterLabelEdit);
51
            this.listView_Drives.AfterLabelEdit += new LabelEditEventHandler(listView_Drives_AfterLabelEdit);
48
 
52
 
49
            // KeyPreview events for keyboard shortcuts
53
            // KeyPreview events for keyboard shortcuts
50
            this.KeyPreview = true;
54
            this.KeyPreview = true;
51
            this.KeyPress += new KeyPressEventHandler(MainForm_KeyPress);
55
            this.KeyDown += new KeyEventHandler(MainForm_KeyDown);
52
            
56
            
53
            // Appends new session start text to log file
57
            // Appends new session start text to log file
54
            using (StreamWriter sw = File.AppendText(logLocation))
58
            using (StreamWriter sw = File.AppendText(logLocation))
55
            {
59
            {
56
                sw.WriteLine("-- New Session Started --");
60
                sw.WriteLine("-- New Session Started --");
Line 71... Line 75...
71
            BackgroundWorker bgWorker = new BackgroundWorker();
75
            BackgroundWorker bgWorker = new BackgroundWorker();
72
            bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
76
            bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
73
 
77
 
74
            // Starts the background worker thread
78
            // Starts the background worker thread
75
            bgWorker.RunWorkerAsync();
79
            bgWorker.RunWorkerAsync();
-
 
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
 
76
        }
94
        }
77
        void bgWorker_DoWork(object sender, DoWorkEventArgs e)
95
        void bgWorker_DoWork(object sender, DoWorkEventArgs e)
78
        {
96
        {
79
            // currentDrives holds a list of 'previous' drives to compare to
97
            // currentDrives holds a list of 'previous' drives to compare to
80
            List<string> currentDrives = new List<string>();
98
            List<string> currentDrives = new List<string>();
Line 109... Line 127...
109
                            }
127
                            }
110
                        }
128
                        }
111
                        // If list mismatch is detected, remove from driveList
129
                        // If list mismatch is detected, remove from driveList
112
                        if (removedDriveFound == false)
130
                        if (removedDriveFound == false)
113
                        {
131
                        {
-
 
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
 
114
                            driveRemoved(str);
146
                            paintDriveListbox();
115
                        }
147
                        }
116
                    }
148
                    }
117
                    // Clears and refreshes the currentDrives list
149
                    // Clears and refreshes the currentDrives list
118
                    currentDrives.Clear();
150
                    currentDrives.Clear();
119
                    foreach (DriveInfo drive in newDrives)
151
                    foreach (DriveInfo drive in newDrives)
Line 132... Line 164...
132
                }
164
                }
133
                // Sleeps the thread for a second
165
                // Sleeps the thread for a second
134
                System.Threading.Thread.Sleep(deviceRemovalRefreshInterval);
166
                System.Threading.Thread.Sleep(deviceRemovalRefreshInterval);
135
            }
167
            }
136
        }
168
        }
137
        private void driveRemoved(string str)
169
        void driveDetector_DeviceArrived(object sender, DriveDetectorEventArgs e)
138
        {
170
        {
-
 
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
 
139
            // Removes the passed drive from driveList
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();
140
            foreach (DriveEntry entry in driveList)
183
            foreach (DriveInfo drive in allDrives)
141
            {
184
            {
142
                if (str == entry.drive)
185
                if (drive.IsReady)
143
                {
186
                {
144
                    driveList.Remove(entry);
187
                    if (drive.Name == newEntry.drive)
145
                    using (StreamWriter sw = File.AppendText(logLocation))
-
 
146
                    {
188
                    {
147
                        sw.WriteLine("Drive Removed --  [" + entry.time.ToString() + "]\t" + entry.drive + "\t\"" + entry.label + "\"\t" + entry.size);
189
                        tempDrive = drive;
-
 
190
                        break;
148
                    }
191
                    }
149
                    break;
-
 
150
                }
192
                }
151
            }
193
            }
-
 
194
            newEntry.label = tempDrive.VolumeLabel;
-
 
195
 
-
 
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
            }
152
 
226
 
153
            paintDriveListbox();
227
            paintDriveListbox();
154
        }
228
        }
155
        void listView_Drives_AfterLabelEdit(object sender, LabelEditEventArgs e)
229
        void listView_Drives_AfterLabelEdit(object sender, LabelEditEventArgs e)
156
        {
230
        {
Line 163... Line 237...
163
 
237
 
164
                    sw.WriteLine("Label \"" + e.Label + "\" added to drive " + entry.SubItems[2].Text);
238
                    sw.WriteLine("Label \"" + e.Label + "\" added to drive " + entry.SubItems[2].Text);
165
                }
239
                }
166
            }
240
            }
167
        }
241
        }
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()
242
        private void paintDriveListbox()
179
        {
243
        {
180
            // Updates the listview
244
            // Updates the listview
181
            this.listView_Drives.BeginUpdate();
245
            this.listView_Drives.BeginUpdate();
182
 
246
 
Line 206... Line 270...
206
                }
270
                }
207
            }
271
            }
208
 
272
 
209
            this.listView_Drives.EndUpdate();
273
            this.listView_Drives.EndUpdate();
210
        }
274
        }
211
        void driveDetector_DeviceArrived(object sender, DriveDetectorEventArgs e)
275
        private void refreshDrives()
212
        {
276
        {
213
            // Event call for when a new drive is detected by DriveDetector
-
 
214
 
-
 
215
            // Disable e.HookQueryRemoved to prevent a hook being attached to the volume
-
 
216
            //e.HookQueryRemove = true;
277
            bool removedDriveFound;
217
 
-
 
218
            // Creates and populates a new DriveEntry
-
 
219
            DriveEntry newEntry = new DriveEntry();
278
            List<DriveEntry> drivesToRemove = new List<DriveEntry>();
220
            newEntry.time = DateTime.Now;
279
            // Checks each entry in driveList to see if matching drive is found on list
221
            newEntry.drive = e.Drive;
280
            // of current drives. Removes entry from driveList if drive is not found.
222
 
-
 
223
            DriveInfo tempDrive = null;
281
            DriveInfo[] currentDrives = DriveInfo.GetDrives();
224
            DriveInfo[] allDrives = DriveInfo.GetDrives();
282
            for (int i = 0; i < driveList.Count; i++)
225
            foreach (DriveInfo drive in allDrives)
283
            //foreach (DriveEntry storedDrives in driveList)
226
            {
284
            {
-
 
285
                DriveEntry storedDrive = driveList[i];
227
                if (drive.IsReady)
286
                removedDriveFound = false;
-
 
287
                // Loop here checks for non-matching entries in the two lists
-
 
288
                foreach (DriveInfo currentDrive in currentDrives)
228
                {
289
                {
-
 
290
                    // If entries match, drive was not removed
229
                    if (drive.Name == newEntry.drive)
291
                    if (storedDrive.drive == currentDrive.Name)
230
                    {
292
                    {
231
                        tempDrive = drive;
293
                        removedDriveFound = true;
232
                        break;
294
                        break;
233
                    }
295
                    }
234
                }
296
                }
-
 
297
                // If list mismatch is detected, remove from driveList
-
 
298
                if (removedDriveFound == false)
-
 
299
                {
-
 
300
                    drivesToRemove.Add(storedDrive);
-
 
301
                }
235
            }
302
            }
236
            newEntry.label = tempDrive.VolumeLabel;
-
 
237
            newEntry.size = (tempDrive.TotalSize / 1073741824).ToString() + " GB";
-
 
238
 
-
 
239
            // Pops up a dialog asking for a new label unless the partition is a system partition
-
 
240
            if (newEntry.label != "System Reserved")
-
 
241
            {
-
 
242
                LabelPrompt label = new LabelPrompt();
-
 
243
                label.ShowDialog();
-
 
244
                newEntry.owner = label.driveLabel;
303
            // Removes drive from driveList
245
            }
-
 
246
            else
-
 
247
                newEntry.owner = "System Reserved";
-
 
248
 
-
 
249
            // Adds the new DriveEntry into driveList
304
            foreach (DriveEntry entry in drivesToRemove)
250
            driveList.Add(newEntry);
-
 
251
 
-
 
252
            using (StreamWriter sw = File.AppendText(logLocation))
-
 
253
            {
305
            {
-
 
306
                // Removes drive from driveList
254
                sw.WriteLine("Drive Attached -- [" + newEntry.time.ToString() + "]\t\"" + newEntry.owner + "\"\t" + newEntry.drive + "\t\"" + newEntry.label + "\"\t" + newEntry.size);
307
                foreach (DriveEntry entry2 in driveList)
-
 
308
                {
-
 
309
                    if (entry.drive == entry2.drive)
-
 
310
                    {
-
 
311
                        driveList.Remove(entry);
-
 
312
                        break;
-
 
313
                    }
-
 
314
                }
255
            }
315
            }
256
 
316
 
257
            paintDriveListbox();
317
            paintDriveListbox();
258
        }
318
        }
259
        //void driveDetector_DeviceRemoved(object sender, DriveDetectorEventArgs e)
319
        //void driveDetector_DeviceRemoved(object sender, DriveDetectorEventArgs e)