C # + WMI + LPT help!

I am making an application that should list all the LPT ports in a machine with I / O addresses. (i.e. exit: LPT1 [start, end] ....)

Using WMI, you can get this information .. name / number from Win32_ParallelPort and addresses from Win32_PortResource.

The problem is that I do not know how to associate a port name with its addresses.

+4
source share
1 answer

You must query three times and iterate over the results to get the corresponding entries from ParallelPort, PnPAllocatedResource and PortResource. The following code does just that:

var parallelPort = new ManagementObjectSearcher("Select * From Win32_ParallelPort"); //Dump(parallelPort.Get()); foreach (var rec in parallelPort.Get()) { var wql = "Select * From Win32_PnPAllocatedResource"; var pnp = new ManagementObjectSearcher(wql); var searchTerm = rec.Properties["PNPDeviceId"].Value.ToString(); // compensate for escaping searchTerm = searchTerm.Replace(@"\", @"\\"); foreach (var pnpRec in pnp.Get()) { var objRef = pnpRec.Properties["dependent"].Value.ToString(); var antref = pnpRec.Properties["antecedent"].Value.ToString(); if (objRef.Contains(searchTerm)) { var wqlPort = "Select * From Win32_PortResource"; var port = new ManagementObjectSearcher(wqlPort); foreach (var portRec in port.Get()) { if (portRec.ToString() == antref) { Console.WriteLine( "{0} [{1};{2}]", rec.Properties["Name"].Value, portRec.Properties["StartingAddress"].Value, portRec.Properties["EndingAddress"].Value ); } } } } } 
+1
source

Source: https://habr.com/ru/post/1338877/


All Articles