2012-11-01 90 views
2

我已經寫了下面的代碼,它沒有得到遠程打印機名稱任何人都可以幫我嗎?如何獲得C#中的所有網絡打印機#

var searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM 
              Win32_Printer"); 

var results = searcher.Get(); 

IList<ManagementBaseObject> printers = new List<ManagementBaseObject>(); 
`enter code here` 
foreach (var printer in results) `enter code here`{ 
    if ((bool)printer["Network"]) `enter code here`{`enter code here` 
     printers.Add(printer);`enter code here` 
    } 
} 

回答

0

我認爲您必須更新您的代碼,如下所示。

private void btnGetPrinters_Click(object sender, EventArgs e) 
{ 
    // Use the ObjectQuery to get the list of configured printers 
    var oquery = new System.Management.ObjectQuery("SELECT * FROM Win32_Printer"); 

    var mosearcher = new System.Management.ManagementObjectSearcher(oquery); 

    System.Management.ManagementObjectCollection moc = mosearcher.Get(); 

    foreach (ManagementObject mo in moc) 
    { 
     System.Management.PropertyDataCollection pdc = mo.Properties; 

     foreach (System.Management.PropertyData pd in pdc) 
     { 
      if ((bool)mo["Network"]) 
      { 
       cmbPrinters.Items.Add(mo[pd.Name]); 
      } 
     } 
    } 
} 

我複製它從Retrieve a List of Installed Printers on the Network Using WMI, C# and VB.NET

相關問題