c# - 使用windows服务和c#检测USB驱动器插入和移除

标签 c# .net windows windows-services wmi

研究制作USB分布式应用程序的可能性
将在插入 U 盘时自动启动并在移除 U 盘时自动关闭

将使用 .Net 和 C#。
正在寻找如何使用 C# 解决此问题的建议?


更新:将此作为服务实现的两种可能的解决方案。
- 覆盖 WndProc
或者
- 将 WMI 查询与 ManagementEventWatcher

结合使用

最佳答案

您可以使用 WMI,它很简单,而且比带有服务的 WndProc 解决方案要好得多。

这是一个简单的例子:

using System.Management;

ManagementEventWatcher watcher = new ManagementEventWatcher();
WqlEventQuery query = new WqlEventQuery("SELECT * FROM Win32_VolumeChangeEvent WHERE EventType = 2");
watcher.EventArrived += new EventArrivedEventHandler(watcher_EventArrived);
watcher.Query = query;
watcher.Start();
watcher.WaitForNextEvent();

关于c# - 使用windows服务和c#检测USB驱动器插入和移除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31186448/

相关文章:

c# - 为什么 KeyValueConfigurationCollection 没有密封?

.net - 流畅的 NHibernate 与开发/测试/生产数据库?

.net - NHibernate CreateAlias - 连接任意列

c# - 使用单个集合来保存所有参数

c# - 用 C 重写 GetHashCode32(string s) 方法

c# - 无法在单元测试中加载文件或程序集 'Newtonsoft.Json,版本 = 6.0.0.0

java - 选择要在 Windows 上运行的 Java 可执行文件

c# - NET 6 : Windows Service with strongly typed configuration

c# - 如果 UDP 数据包在线上,我能保证在应用层得到它们吗?

c# - 捕捉声音输出