2011-02-08 12 views
4

IOアドレスを持つマシンのすべてのLPTポートを一覧表示する必要があるアプリケーションを作成しています。 (出力:LPT1 [開始;終了] ....)C#+ WMI + LPTヘルプ!

WMIを使用すると、Win32_ParallelPortの名前と番号、およびWin32_PortResourceのアドレスを取得できます。

問題は、ポート名とそのアドレスを関連付ける方法がわかりません。

+1

この質問は、Delphiについてですが、質問のWMIの面について多くの有益な情報を抽出できるはずです。http://stackoverflow.com/questions/2386169/find-available-parallel-ports-and-their-io-addresses-delphi-and-wmiを使用する –

答えて

1

ParallelPort、PnPAllocatedResource、PortResourceから一致するレコードを取得するには、結果を3回クエリしてループする必要があります。次のコードはまさにそれを行います:

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); 
       } 
      } 
     } 
    } 
}