【问题标题】:How to use a FileSystemWatcher for monitoring new drives [duplicate]如何使用 FileSystemWatcher 监视新驱动器 [重复]
【发布时间】:2011-08-29 14:52:06
【问题描述】:

可能重复:
Looking for C# code for detecting removable drive (usb flash)

System.IO.FileSystemWatcher 类如何用于检测 Windows 下连接/断开的驱动器(例如 F:)?如果这不可能,还有哪些其他的事件驱动方式(除了轮询DriveInfo.GetDrives())?

提前致谢。

【问题讨论】:

标签: c# .net windows filesystemwatcher


【解决方案1】:
【解决方案2】:

我编写了一个 powershell 模块,它使用 System.Management.ManagementEventWatcher 和 WMI 类 Win32_VolumeChangedEvent 来显示您可能在 powershell 中注册的新事件,包括设备移除、添加等。您应该能够从我的这篇博文:

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

您应该能够使用我在上述脚本中使用的方法,在不到 10 行 C# 代码中为新驱动器连接一个事件。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-07
    • 1970-01-01
    • 1970-01-01
    • 2018-07-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多