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?
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?
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 get it, I’m going to give a study here, thank you very much
0
I gave a search and found this code, it will play on variable drivers, all the removable type drivers that are have been connected.
var drives = DriveInfo.GetDrives()
.Where(drive => drive.IsReady && drive.DriveType == DriveType.Removable);
Source:https://stackoverflow.com/questions/6003822/how-to-detect-a-usb-drive-has-been-plugged-in
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 c# .net api
You are not signed in. Login or sign up in order to post.
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?
– Maniero
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.
– meisterx7
Or I could create a timer that runs from second to second, until I identify that the disk has been connected
– meisterx7
Could, but it’s a bad solution. I’ll try to answer.
– Maniero
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.
– Bacco
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.
– meisterx7
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?
– Maniero