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 win开发者_如何学Godows? If this is not possible, what other event driven ways are possible (other than polling DriveInfo.GetDrives()
)?
Thanks in advance.
Take a look at this:
http://www.codeproject.com/KB/system/DriveDetector.aspx
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.
精彩评论