Identify pendrive

2

I need the application to copy a file when a pendrive is connected. Can you identify when the removable disk is connected and the volume name so that the system does not copy to the wrong pendrive?

    
asked by anonymous 22.01.2015 / 18:43

2 answers

4

It's not a trivial task so it's hard to put a complete solution here on the site . The way is to use the Windows API to build a detection mechanism. I found some examples on the internet. What I found most promising is this complete project in CodeProject .

I'm not guaranteeing that it will work for you and that you do not have to make changes, but the description of the project that I have seen several recommendations indicates meets the needs you described. What I can see is that it actually waits for Windows to notify you via the WndProc method that is the default Windows messaging. And the parameters used, especially of the message WM_DEVICECHANGE , are consistent with what is proposed.

From what I've noticed, it's easy to customize with what you need and set a good example.

To get the volume just use the DriveInfo.VolumeLabel :

var allDrives = DriveInfo.GetDrives();
foreach (var d in allDrives) {
    if (d.IsReady) {
        Console.WriteLine("Volume label: {0}", d.VolumeLabel);
    }
}
    
22.01.2015 / 19:40
0

I gave a search and found this code, it will play on the variable drivers, all the removable type drivers that are connected.

var drives = DriveInfo.GetDrives()
    .Where(drive => drive.IsReady && drive.DriveType == DriveType.Removable);

Source: link

    
22.01.2015 / 19:16