.Net code to Detect When an External Drive is Mounted?

S

Sped

Hi,

I'm looking for some code to monitor for when an external drive is
connected to a computer (USB ThumbDrive, External Hard Drive, etc.) and
is assigned a drive letter.

I'd want my code to trigger off of the mounting process so I can search
for a specific file on that hard drive (which is always guaranteed to
be in a specific spot).

Is this a Windows Event I can attach to?

Either Vb.net or C# code would be extremely helpful.

Thanks,
(e-mail address removed)
(sorry for the cross-post - not sure what groups are monitored these
days)
 
?

=?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?=

Sped said:
Hi,

I'm looking for some code to monitor for when an external drive is
connected to a computer (USB ThumbDrive, External Hard Drive, etc.) and
is assigned a drive letter.
<snip>

on your form:

private const Int32 DBT_DEVICEARRIVAL = 0x8000;
private const Int32 DBT_DEVICEREMOVECOMPLETE = 0x8004;
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WindowsMessages.WM_DEVICECHANGE)
{
switch (m.WParam.ToInt32())
{
case WindowsMessageConstants.DBT_DEVICEARRIVAL:
// Handle disconnected device here
break;
case DBT_DEVICEREMOVECOMPLETE:
// Handle connected device here
break;
}
}
}

afaik, there's no way to get from the data in the message struct which
device was (dis)connected but you can easily scan through drives and
compare to previous list to find out which one that (dis)appeared.

Hope this helps.
 
C

Chris R. Timmons

Hi,

I'm looking for some code to monitor for when an external drive
is connected to a computer (USB ThumbDrive, External Hard Drive,
etc.) and is assigned a drive letter.

I'd want my code to trigger off of the mounting process so I can
search for a specific file on that hard drive (which is always
guaranteed to be in a specific spot).

Is this a Windows Event I can attach to?

Either Vb.net or C# code would be extremely helpful.


http://groups-beta.google.com/group...ublic.dotnet.*&as_scoring=d&lr=&num=100&hl=en

or

http://tinyurl.com/5s7po
 

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