使用windows服务和c#检测USB驱动器的插入和移除 [英] Detecting USB drive insertion and removal using windows service and c#

查看:49
本文介绍了使用windows服务和c#检测USB驱动器的插入和移除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Looking into possibility of making an USB distributed application
that will autostart on insertion of an USB stick and shutdown when removing the stick

将使用 .Net 和 C#.
寻找如何使用 C# 解决这个问题的建议?

Will use .Net and C#.
Looking for suggestion how to approach this using C#?


更新:将此作为服务实现的两种可能的解决方案.
- 覆盖 WndProc

- 在 ManagementEventWatcher 中使用 WMI 查询


Update: Two possible solutions implementing this as a service.
- override WndProc
or
- using WMI query with ManagementEventWatcher

推荐答案

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

You can use WMI, it is easy and it works a lot better than WndProc solution with services.

这是一个简单的例子:

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();

这篇关于使用windows服务和c#检测USB驱动器的插入和移除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆