Visual C# -> WMI/ANY -> Volume Information (No LABEL, No Mountpoint)
i need list removable media only.
equipment / hardware:
- uas storage (usb -> scsi).
- usb attached (sd/md/etc) card controller.
- usb storage.
- nic attached storage.
partitioning:
- all devices has 3 or more partitions, commonly 4-5.
- 1 primary-active, second partitions primary or 4 primary/3 primary + 1 extended maximum.
- labels = often: null
- drive letters: can no mount point.
needed information wmic:
- partitions or without mount point or label.
- volume letters [if existing -> if not = partition position, size , type(primary / logical)].
- order data start-end.
question:
do have easier way above listing going through wmi:
- win32_diskdrive -> win32_diskdrivetodiskpartition
- win32_controller
- win32_pnp -> ???
- win32_volume (still don't know find volume guid, 'mountvol' drive without mount point can't parse if there's more 1 partition without mount point).
- win32_logicaldisktopartition -> win32_logicaldisk
heck, counted that, if type whole query chain it's gonna take long execute can have dinner before program gets started. ;)
------edit #1------
found 1 unreliable solution wmi connection using registry.
path:
- win32_disk....
- win32_...controller
- win32_pnpentity
- registry, key: hkcu\software\microsoft\windows\currentversion\explorer\mountpoints2\cpc\volume\{volumeid}, value: data, reg_binary
- win32_volume
wouldn't call reliable solution, see it, atm, solution looking through network people searching solution same problem. unless guru here can me bit. ;)
also looking @ kernel32.dll methods , 'getvolumenameforvolumemountpoint' similar topics mine requires either 'volume name' or 'drive letter' came bit more simplistic solution link wmi resources windows registry seems @ least @ systems being maintained device removal/insert. of course dump diskpart info , parse that.
------edit #2------
found bit better solution thru wmi namespace called: root\microsoft\windows\storage\providers_v2
as digging thru powershell solutions. namespace seems give:
- wsp_disk
- wsp_disktopartition
- wsp_partition
- wsp_partitiontovolume
- wsp_volume
which again linkable or usable directly. not sure how far these goes. have test xp / server variants.
since far searched web so, many howto's translate microsoft reg_binary -> text. here example visual c# code used try above linking. waste (but works ;p).
private string regbintotext() { registrykey regkey = registry.currentuser.opensubkey("software").opensubkey("mycompany"); byte[] keys = (byte[])regkey.getvalue("regdata"); stringbuilder sb = new stringbuilder(keys.length * 2); foreach (byte key in keys) { sb.appendformat("{0:x2}", key); } string remzeros = sb.tostring().replace("00", ""); var builder = new stringbuilder(); (int = 0; < remzeros.length; += 2) { string hexdec = remzeros.substring(i, 2); int number = int32.parse(hexdec, system.globalization.numberstyles.hexnumber); char chartoadd = (char)number; builder.append(chartoadd); } var splitter = builder.tostring().replace("starting_string", "\nstarting_string").replace("ending_string", "\nending_string"); string[] finalize_split = splitter.split('\n'); foreach(string final in finalize_split) { return final; } return null; }
basically, if want information 'final' string put if statement like:
if(final.startswith("yourlinestarts")) { string query = final; return query; }
inside foreach loop.
foreach(string final in finalize_split)
-- question --
does here know anyway build query escapes content below:
try { managementobjectsearcher searcher = new managementobjectsearcher("root\\cimv2", "select * win32_diskdrivetodiskpartition antecedent = \\\\computer\\root\\cimv2:win32_diskdrive.deviceid="\\\\\\\\.\\\\physicaldrive0""); foreach (managementobject queryobj in searcher.get()) { console.writeline("-----------------------------------"); console.writeline("win32_diskdrivetodiskpartition instance"); console.writeline("-----------------------------------"); console.writeline("antecedent: {0}", queryobj["antecedent"]); console.writeline("dependent: {0}", queryobj["dependent"]); } } catch (managementexception e) { messagebox.show("an error occurred while querying wmi data: " + e.message); }
this example directly microsoft wmi code creator failing content '\' clause/statement. yeah, been reading quite bit escaping , on, when escape correctly wmi returns: "invalid query"
?
correct looks like:'"select * win32_diskdrivetodiskpartition antecedent = \\\\computer\\root\\cimv2:win32_diskdrive.deviceid = \"\\\\.\\physicaldrive0\""'
Visual Studio Languages , .NET Framework > Visual C#
Comments
Post a Comment