2

Possible Duplicate:
How to detect a USB device has been plugged in?

I want to create c# .net windows application in which i want to show message when any USB device is connected to the local machine.

Also when this USB device is been connected i want to read a particular file in it.

My problem is that how should i determine the drive name of USB for reading file.

Eg.On particular machine if there are only two drives (c and d)...the usb drive detected over there wil be e. but on another machine if there are 3 drives then in that case USB drive can be f.

What shoud i do in that case for showing the message on connection of USB drive and reading the particular file in that drive.

Community
  • 1
  • 1
Freelancer
  • 9,008
  • 7
  • 42
  • 81

1 Answers1

4

For your first question check this link: How do I detect when a removable disk is inserted using C#?

For the other part of your question:

Also when this USB device is been connected i want to read a particular file in it.

You can get the drive letter and the volume label of all the removable drives connected to your system like this :

  DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
if (d.DriveType == DriveType.Removable) 
{   
  Console.WriteLine("Drive {0}", d.Name);       
        Console.WriteLine("  Volume label: {0}", d.VolumeLabel);
}
}  

If the volume label of the USB drive is going to be same then its not a problem else you will have to search for the required file in all the removable drives connected to your system.
Hope it helps.

Community
  • 1
  • 1
Saurabh R S
  • 3,037
  • 1
  • 34
  • 44