gpt4 book ai didi

c# - 如何在 C# 中获取新插入的 USB 驱动器盘符?

转载 作者:行者123 更新时间:2023-11-30 12:57:03 25 4
gpt4 key购买 nike

我写了一个 c# 程序来查找新插入的 USB 驱动器及其驱动器号。现在,当我运行这个程序时,我得到了插入事件,但无法得到驱动器号。谁能建议我这样做的想法?

代码

static void Main(string[] args)
{
ManagementEventWatcher mwe_creation; //Object creation for 'ManagementEventWatcher' class is used to listen the temporary system event notofications based on specific query.
WqlEventQuery q_creation = new WqlEventQuery(); //Represents WMI(Windows Management Instrumentation) event query in WQL format for more information goto www.en.wikipedia.org/wiki/WQL
q_creation.EventClassName = "__InstanceCreationEvent";// Sets the eventclass to the query
q_creation.WithinInterval = new TimeSpan(0, 0, 2); // Setting up the time interval for the event check(here, it is 2 Seconds)
q_creation.Condition = @"TargetInstance ISA 'Win32_DiskDriveToDiskPartition'"; //Sets which kind of event to be notified
mwe_creation = new ManagementEventWatcher(q_creation); //Initializing new instance
mwe_creation.EventArrived += new EventArrivedEventHandler(USBEventArrived_Creation);//Calling up 'USBEventArrived_Creation' method when the usb storage plug-in event occured
mwe_creation.Start(); // Starting to listen to the system events based on the given query
while (true) ;

}
static void USBEventArrived_Creation(object sender, EventArrivedEventArgs e){

Console.WriteLine("USB PLUGGED IN!");
ManagementBaseObject instance = (ManagementBaseObject)e.NewEvent["TargetInstance"];
foreach (var property in instance.Properties)
{

if (property.Name == "Name")
Console.WriteLine(property.Name + " = " + property.Value);
}

}

最佳答案

您可能工作太过努力,无法重新创造一个已有的灵魂。这是由 Stephen Mcohn 制作的公共(public)许可项目它提供了您需要的界面并且似乎有很好的文档记录:

http://www.codeproject.com/Articles/63878/Enumerate-and-Auto-Detect-USB-Drives

以下是他如何仅过滤 USB 驱动器

new ManagementObjectSearcher(
"select DeviceID, Model from Win32_DiskDrive " +
"where InterfaceType='USB'").Get())

这是恢复特定驱动器盘符的方式

使用 Associators 完成获取特定的驱动器号获取 Win32_LogicalDisk,然后它可以提供设备 ID(例如驱动器号)。

Microsoft 提供了一个 VB 代码示例,如果您不想只导入 Stephen 的整个模块,您可以移植它:

ComputerName = "."
Set wmiServices = GetObject ( _
"winmgmts:{impersonationLevel=Impersonate}!//" & ComputerName)
' Get physical disk drive
Set wmiDiskDrives = wmiServices.ExecQuery ( "SELECT Caption, DeviceID FROM Win32_DiskDrive")

For Each wmiDiskDrive In wmiDiskDrives
WScript.Echo "Disk drive Caption: " & wmiDiskDrive.Caption & VbNewLine & "DeviceID: " & " (" & wmiDiskDrive.DeviceID & ")"

'Use the disk drive device id to
' find associated partition
query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" _
& wmiDiskDrive.DeviceID & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition"
Set wmiDiskPartitions = wmiServices.ExecQuery(query)

For Each wmiDiskPartition In wmiDiskPartitions
'Use partition device id to find logical disk
Set wmiLogicalDisks = wmiServices.ExecQuery _
("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" _
& wmiDiskPartition.DeviceID & "'} WHERE AssocClass = Win32_LogicalDiskToPartition")

For Each wmiLogicalDisk In wmiLogicalDisks
WScript.Echo "Drive letter associated" _
& " with disk drive = " _
& wmiDiskDrive.Caption _
& wmiDiskDrive.DeviceID _
& VbNewLine & " Partition = " _
& wmiDiskPartition.DeviceID _
& VbNewLine & " is " _
& wmiLogicalDisk.DeviceID
Next
Next
Next

关于c# - 如何在 C# 中获取新插入的 USB 驱动器盘符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36833296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com