本文介绍了使用Windows服务和C#检测U盘插入和移除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
展望做一个USB分布式应用程序
的可能性这将去除粘
时,自动启动上插入一个USB记忆棒和关闭的
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
或
- 使用WMI查询与ManagementEventWatcher
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.
下面是一个简单的例子:
Here is a simple example:
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();
就是这样:)
And that's it :)
这篇关于使用Windows服务和C#检测U盘插入和移除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!