Is it possible to get notified (without polling, but via an event) when a drive letter becomes accessible. For example if you have an external hard drive that always appears as drive F - is it possible to have an event raised when that is connected and F becomes accessible?
-
Okay.. found what I was looking for :)
Take a look at this VBScript: (source):
strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colEvents = objWMIService.ExecNotificationQuery _ ("Select * From __InstanceOperationEvent Within 10 Where " _ & "TargetInstance isa 'Win32_LogicalDisk'") Do While True Set objEvent = colEvents.NextEvent If objEvent.TargetInstance.DriveType = 2 Then Select Case objEvent.Path_.Class Case "__InstanceCreationEvent" Wscript.Echo "Drive " & objEvent.TargetInstance.DeviceId & _ " has been added." Case "__InstanceDeletionEvent" Wscript.Echo "Drive " & objEvent.TargetInstance.DeviceId & _ " has been removed." End Select End If Loop
I leave it to your exercise to port it to C#.
Instead of polling all the time you can use a WMI event sink.
From VVS -
You can wait for the WM_DEVICECHANGE message, all the details are at:
http://msdn.microsoft.com/en-us/library/aa363215(VS.85).aspx
You're going to have to create a window to receive this message, the window can be hidden if you need, to get this message in WinForms just override the Form.WndProc method
From Nir
0 comments:
Post a Comment