ManagmentObject queries for windows (C#) to find usb device -
i'm trying find particular usb device (1 or more) connected computer , retrieve relevant path mounted drive. ideally, finding vid/pid of usb device, i'm not sure how yet. following works, there must way data in single query.
what i'm doing here looking or physical drive has model matching hs sd card bridge usb device
, finding physical drive # associated , using find mounted partition..
foreach (managementobject disk in disks.get()) { //look drives match our string match m = regex.match(disk["model"].tostring(), "hs sd card bridge usb device"); if (m.success) { m = regex.match(disk["deviceid"].tostring(), @"physicaldrive(\d+)"); if (m.success) { int drivenumber = int32.parse(m.groups[1].tostring()); managementobjectsearcher mapping = new managementobjectsearcher("select * win32_logicaldisktopartition"); foreach (managementobject map in mapping.get()) { m = regex.match(map["antecedent"].tostring(), @"disk #" + drivenumber + ","); if (m.success) { string drive = map["dependent"].tostring(); m = regex.match(drive, @"([a-z]):"); if (m.success) { drive = m.groups[1].tostring(); //< -- **found** } } } //usbdevice dev = new usbdevice("", ""); // list.items.add(); console.writeline(""); } } }
is there way vid/pid , way construct search query requires 1 query?
this 1 used earlier . not answer. .
public int getavailabledisks() { int devicefound = 0; try { // browse usb wmi physical disks foreach (managementobject drive in new managementobjectsearcher( "select deviceid, model win32_diskdrive interfacetype='usb'").get()) { managementobject partition = new managementobjectsearcher(string.format( "associators of {{win32_diskdrive.deviceid='{0}'}} assocclass = win32_diskdrivetodiskpartition", drive["deviceid"])).first(); if (partition == null) continue; // associate partitions logical disks (drive letter volumes) managementobject logical = new managementobjectsearcher(string.format( "associators of {{win32_diskpartition.deviceid='{0}'}} assocclass = win32_logicaldisktopartition", partition["deviceid"])).first(); if (logical != null) { // find logical disk entry determine volume name - not necesssary //managementobject volume = new managementobjectsearcher(string.format( // "select freespace, size, volumename win32_logicaldisk name='{0}'", // logical["name"])).first(); string temp = logical["name"].tostring() + "\\"; // +" " + volume["volumename"].tostring(); future purpose if device name required devicefound++; if (devicefound > 1) { messagebox.show(@"multiple removeable media found. please remove device"); devicefound--; } else { drivename = temp; } } } } catch (exception diskenumerateexception) { } return devicefound; }
Comments
Post a Comment