1

Possible Duplicate:
Looking for C# code for detecting removable drive (usb flash)

How could the System.IO.FileSystemWatcher class be used to detect drives (e.g. F:) being connected/disconnected under windows? If this is not possible, what other event driven ways are possible (other than polling DriveInfo.GetDrives())?

Thanks in advance.

Community
  • 1
  • 1
James
  • 30,496
  • 19
  • 86
  • 113
  • 6
    StackOverflow rocks: http://stackoverflow.com/questions/715739/looking-for-c-code-for-detecting-removable-drive-usb-flash – Davide Piras Aug 29 '11 at 14:53

2 Answers2

4

Take a look at this:

http://www.codeproject.com/KB/system/DriveDetector.aspx

Madhur Ahuja
  • 22,211
  • 14
  • 71
  • 124
0

I wrote a powershell module that uses a System.Management.ManagementEventWatcher and the WMI class Win32_VolumeChangedEvent to surface new events that you may register for within powershell covering device removal, addition etc. You should be able to figure out the relevant plumbing from this blog post of mine:

http://www.nivot.org/nivot2/post/2008/08/16/AutoMountunmountNewPSDrivesForRemovableDrivesAndNetworkSharesInPowerShellV2.aspx

You should be able to wire up an event for new drives in less than ten lines of C# using the methods I use in the above script.

Hope this helps.

x0n
  • 51,312
  • 7
  • 89
  • 111