473,513 Members | 2,461 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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,
Sp*******@erstads.com
(sorry for the cross-post - not sure what groups are monitored these
days)

Nov 16 '05 #1
3 3755
Sped wrote:
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.

--
Lasse Vågsæther Karlsen
http://www.vkarlsen.no/
mailto:la***@vkarlsen.no
PGP KeyID: 0x0270466B
Nov 16 '05 #2
"Sped" <sp*******@erstads.com> wrote in
news:11*********************@f14g2000cwb.googlegro ups.com:
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/groups...&num=100&hl=en

or

http://tinyurl.com/5s7po

--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 16 '05 #3
Thanks for the reponses - I'll follow up on those.

Here's another good link that I found that appears to work swell (C#
code):

http://www.codecomments.com/archive2...-4-185490.html

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

242
13135
by: James Cameron | last post by:
Hi I'm developing a program and the client is worried about future reuse of the code. Say 5, 10, 15 years down the road. This will be a major factor in selecting the development language. Any...
0
1265
by: Eric Haskins | last post by:
I had a server lose a drive. This customer didnt back his database. I mounted old failing drive as a slave and can see .frm .MYI & .MYD But if I copy them to /var/lib/mysql/db_name it says .frm...
45
3556
by: Steven T. Hatton | last post by:
This is a purely *hypothetical* question. That means, it's /pretend/, CP. ;-) If you were forced at gunpoint to put all your code in classes, rather than in namespace scope (obviously classes...
6
5267
by: Kiran | last post by:
Hi, I have program, which opens file at the startup and logs error messages to the file, file handle is closed at the end of the program. However if file is deleted in-between, program do not...
2
4037
by: Sped | last post by:
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...
3
5742
by: eieiohh | last post by:
MySQL 3.23.49 PHP 4.3.8 Apache 2.0.51 Hi All! Newbie.. I had a CRM Open Source application installed and running. Windows Xp crashed. I was able to copy the contents of the entire hard...
8
55667
by: Kevin D. | last post by:
Please note, I already posted this on the MySQL official forum, but received no response. I thought I'd try again in another location. My apologies to anyone reading this twice... Despite...
0
1715
by: D2 | last post by:
Hi, I'm working on a C# windows service that should detect when a USB drive is plugged in. After drive letter has been assigned, start using that drive. On the other side, when user clicks on...
7
1369
by: Malakie | last post by:
Hi all, I am trying to self teach VB .net to myself. Not sure what is worse, trying to accomplish this or thinking I can actually do it! ;-) Anyhow, Would someone be able to give me some...
0
7260
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7161
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7539
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7101
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5686
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4746
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1596
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.