Logical drives types and system events

G

Guest

Hi,

Is there any class that watch for the logical drives installed in the
computer.
I want to enable a button in a windows form application when a USB stick is
inserted and recognised.

For the events I do a loop and always ask for the logical drives and to
differentiate from the local drive I've got this way around but I have to
know which drives are installed before a new one is inserted and I also dont
know what kind of drive it is.
_____________________________________________________________
logicalDrives = System.IO.Directory.GetLogicalDrives()
For Each logicalDrive In logicalDrives
directoryInfo = New System.IO.DirectoryInfo(logicalDrive)
If driveInfo.Exists Then
Dim str As String
str = driveInfo.FullName
'Only removable drives
If str <> "C:\" AND str <> "D:\"Then
found = True
Exit For
End If
End If
Next
________________________________________________________________

I have for example the problem that when I insert a floppy disk, my code
does not notice the diference (USB stick <> Floppy)

Thanks
 
M

Mattias Sjögren

Is there any class that watch for the logical drives installed in the
computer.

Not that I'm aware of, but you can try using the native
RegisterDeviceNotification API and watch for WM_DEVICECHANGE messages.



Mattias
 
G

Guest

It is more complicated as I though. Any way thanks for the input. I am also
reading about the System.Management namespace and the ManagementEventWatcher.
I will have a look on both ideas
 
W

Willy Denoyette [MVP]

Yes, the Management classes are the way to go.
You can register an ManagementEventWatcher for 'Win32_USBControllerDevice'
instance arrivals, sometyhing like this will do:

.....
WqlEventQuery q = new WqlEventQuery();
q.EventClassName = "__InstanceOperationEvent";
q.WithinInterval = new TimeSpan(0,0,3);
q.Condition = @"TargetInstance ISA 'Win32_USBControllerDevice' ";
w = new ManagementEventWatcher(q);
w.EventArrived += new EventArrivedEventHandler(this.UsbEventArrived);
w.Start(); // Start listen for events

public void UsbEventArrived(object sender, EventArrivedEventArgs e) {
.... // Handle the event
}

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top