ManagementObjectSearcher选择网络打印机?

时间:2010-07-16 12:38:27

标签: c# network-printers

我用:

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

获得打印机,但它找到本地打印机.. 可以通过ip选择网络打印机吗?

1 个答案:

答案 0 :(得分:5)

将“网络”传递给ManagementObjectCollection中每个项目的对象索引器。如果打印机是网络打印机,它将返回true。同样,您可以传递“本地”以确定打印机是否为本地打印机。

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

IList<ManagementBaseObject> printers = new List<ManagementBaseObject>();

foreach (var printer in results) {
    if ((bool)printer["Network"]) {
        printers.Add(printer);
    }
}