【发布时间】:2014-11-12 19:00:49
【问题描述】:
按照给定链接How do I detect when a removable disk is inserted using C#?中的建议,我检测到在我的 WPF 应用程序中插入了 USB 驱动器
但我无法确定检测到的 USB 驱动器的驱动器号;我的代码如下所示
static ManagementEventWatcher w = null;
static void AddInsertUSBHandler()
{
WqlEventQuery q;
ManagementScope scope = new ManagementScope("root\\CIMV2");
scope.Options.EnablePrivileges = true;
try {
q = new WqlEventQuery();
q.EventClassName = "__InstanceCreationEvent";
q.WithinInterval = new TimeSpan(0, 0, 3);
q.Condition = "TargetInstance ISA 'Win32_USBControllerdevice'";
w = new ManagementEventWatcher(scope, q);
w.EventArrived += USBInserted;
w.Start();
}
catch (Exception e) {
Console.WriteLine(e.Message);
if (w != null)
{
w.Stop();
}
}
}
static void USBInserted(object sender, EventArgs e)
{
Console.WriteLine("A USB device inserted");
}
如果可能,请指导我。
【问题讨论】:
标签: c#