我正在制作一个应用程序,需要列出机器中所有具有IO地址的LPT端口。 (即它的输出:LPT1 [开始;结束] ....)
使用WMI,您可以获取此信息..来自Win32_ParallelPort的名称/编号以及来自Win32_PortResource的地址。
问题是我不知道如何将portname与其地址相关联。
答案 0 :(得分:1)
您必须查询三次并循环结果才能从ParallelPort,PnPAllocatedResource和PortResource获取匹配的记录。以下代码正是如此:
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 );
}
}
}
}
}