Identify USB stick

Asked

Viewed 1,201 times

2

I need the application to copy a file when a flash drive is connected. It has how to identify when the removable disk is connected and the volume name, so that the system does not copy to the flash drive wrong?

  • You can check if the flash drive is connected or the application needs to be notified that there was the connection? I understood that would be the second case, I’m sure?

  • The application had to be notified. That’s basically it. It is a backup system, then arrived in a specific time, it asks to connect the pen drive, when connect it copies.

  • Or I could create a timer that runs from second to second, until I identify that the disk has been connected

  • Could, but it’s a bad solution. I’ll try to answer.

  • Unrelated to the question, just one note: USB stick is one of the least reliable media to back up. By the time you realize that it has given problem and is returning corrupted data, it is too late.

  • I know this problem, this is just a temporary backup. It copies the data to removable media and at the same time to a server. But thanks for the remark.

  • 1

    Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

Show 2 more comments

2 answers

4

It is not a trivial task so it is difficult to put a complete solution here on website. The way is to use the Windows API to build a detection engine. I found some examples on the internet. What seemed most promising is this complete project in Codeproject.

I am not guaranteeing that it will work for your case and that you do not need to make modifications but the description of the project I have seen several recommendations indicates that it meets the needs you described. What I can see is that it really waits for Windows to notify you through the method WndProc which is the Windows standard for message exchange. And the parameters used, especially the message WM_DEVICECHANGE, are consistent with what is proposed.

From what I understand is easy to customize with what you need and has a good example.

To pick up the volume just use pick up the property DriveInfo.VolumeLabel:

foreach (var d in DriveInfo.GetDrives()) {
    if (d.IsReady) WriteLine($"Volume label: {d.VolumeLabel}");
}

I put in the Github for future reference.

  • I get it, I’m going to give a study here, thank you very much

0

  • This detects the presence or just checks on demand if there is a connected device?

  • Lists those that are connected at the time you run this code

Browser other questions tagged

You are not signed in. Login or sign up in order to post.