正在插入USB驱动器的驱动器号
本文关键字:驱动器 USB 插入 | 更新日期: 2023-09-27 18:30:07
我按照给定链接中的建议检测到WPF应用程序中插入了USB驱动器。如何检测使用C#插入可移动磁盘?
但我无法找出检测到的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");
}
如果可能的话,请引导我。
希望这有帮助,它的代码基于Neeraj Dubbey的答案。
我们在一个连续的多线程循环中运行代码,该循环不断检查连接的USB设备的变化。因为我们会跟踪以前连接的设备,所以我们可以将其与新的设备列表进行比较。我们还可以使用相同的方法来确定是否移除了设备。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Usb_Test
{
class Program
{
private static List<USBDeviceInfo> previousDecices = new List<USBDeviceInfo>();
static void Main(string[] args)
{
Thread thread = new Thread(DeviceDetection);
thread.Start();
Console.Read();
}
static void DeviceDetection()
{
while (true)
{
List<USBDeviceInfo> devices = new List<USBDeviceInfo>();
ManagementObjectCollection collection;
using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub"))
collection = searcher.Get();
foreach (var device in collection)
{
devices.Add(new USBDeviceInfo(
(string)device.GetPropertyValue("DeviceID")
));
}
if (previousDecices == null || !previousDecices.Any()) // So we don't detect already plugged in devices the first time.
previousDecices = devices;
var insertionDevices = devices.Where(d => !previousDecices.Any(d2 => d2.DeviceID == d.DeviceID));
if (insertionDevices != null && insertionDevices.Any())
{
foreach(var value in insertionDevices)
{
Console.WriteLine("Inserted: " + value.DeviceID); // Add your own event for the insertion of devices.
}
}
var removedDevices = previousDecices.Where(d => !devices.Any(d2 => d2.DeviceID == d.DeviceID));
if (removedDevices != null && removedDevices.Any())
{
foreach (var value in removedDevices)
{
Console.WriteLine("Removed: " + value.DeviceID); // Add your own event for the removal of devices.
}
}
previousDecices = devices;
collection.Dispose();
}
}
}
class USBDeviceInfo
{
public USBDeviceInfo(string deviceID)
{
this.DeviceID = deviceID;
}
public string DeviceID { get; private set; }
}
}
然而,这不是一个很好的解决方案,您最好收听WM_DEVICECHANGE。
无论何时,Windows都会向所有应用程序发送WM_DEVICECHANGE消息发生一些硬件更改,包括当闪存驱动器(或其他可移除设备)被插入或移除。此的WParam参数消息包含的代码准确地指定了发生的事件。对于我们的目的只有以下事件是有趣的:
DBT_DEVICEARIVAL-在设备或媒体插入。当设备处于准备使用,大约在资源管理器显示对话框时它允许您选择如何处理插入的介质。
DBT_DEVICEQUERYREMOVE-当系统请求对移除设备或介质。任何应用程序都可以拒绝请求并取消删除。如果你需要在移除闪存驱动器之前对其执行一些操作,例如加密上面的一些文件。您的程序可以拒绝此请求这将导致Windows显示众所周知的消息现在无法删除该设备。
DBT_device_REMOVECOMPLETE-在删除设备后发送。什么时候您的程序收到此事件,设备不再可用--当Windows显示其"设备已删除"气泡时给用户。
以下是一些链接,每个链接都详细说明了如何在C#中做到这一点:
http://www.codeproject.com/Articles/18062/Detecting-USB-Drive-Removal-in-a-C-Programhttp://www.codeproject.com/Articles/60579/A-USB-Library-to-Detect-USB-Devices
这两种解决方案都应该有效,并且您可以随时调整它们以满足您的需求。
嘿,在项目中添加System.Management
的引用后,试试这个基于控制台的代码
namespace ConsoleApplication1
{
using System;
using System.Collections.Generic;
using System.Management; // need to add System.Management to your project references.
class Program
{
static void Main(string[] args)
{
var usbDevices = GetUSBDevices();
foreach (var usbDevice in usbDevices)
{
Console.WriteLine("Device ID: {0}", usbDevice.DeviceID);
}
Console.Read();
}
static List<USBDeviceInfo> GetUSBDevices()
{
List<USBDeviceInfo> devices = new List<USBDeviceInfo>();
ManagementObjectCollection collection;
using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub"))
collection = searcher.Get();
foreach (var device in collection)
{
devices.Add(new USBDeviceInfo(
(string)device.GetPropertyValue("DeviceID")
));
}
collection.Dispose();
return devices;
}
}
class USBDeviceInfo
{
public USBDeviceInfo(string deviceID)
{
this.DeviceID = deviceID;
}
public string DeviceID { get; private set; }
}