开发者

How to use a FileSystemWatcher for monitoring new drives [duplicate]

开发者 https://www.devze.com 2023-04-01 01:23 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Looking for C# code for detecting removable drive (usb flash)
This question already has answers here: Closed 11 years ago.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号