本文介绍了如何获取网络上所有打印机的列表以检查它是联机还是脱机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人可以告诉我如何获取网络上所有打印机的列表吗?
我尝试使用WMI,但以下代码仅向我提供了本地打印机的列表
这是我尝试过的代码.
Can anyone tell me how to get a list of all printers on a network?
I have tried using WMI but the following code only gives me a list of local printers
here is the code that i tried.
//--set the scope of this search to the local machine
ManagementScope scope = new ManagementScope(@"\root\cimv2");
//--connect to the machine
scope.Connect();
//--query for the ManagementObjectSearcher to get all printer
SelectQuery selectQuery1 = new SelectQuery();
selectQuery1.QueryString = "Select * from win32_Printer";
ManagementObjectSearcher obj = new ManagementObjectSearcher(scope, selectQuery1);
ManagementObjectCollection printers = obj.Get();
//--now loop through each printer instance returned
foreach (ManagementObject printer in printers)
{
//--first make sure that there is a printer
if (printer != null)
{
str = printer["Name"].ToString().ToLower();
//--check if it matches the name provided
if (str.ToUpper().Equals("MyPrinterName".ToUpper()))
{
//--since we found a match check it''s status
if (printer["WorkOffline"].ToString().ToLower().Equals("true") || printer["PrinterStatus"].Equals(7))
{
//--it''s offline
online = false;
}
else
{
//--it''s online
online = true;
}
}
}
else
{
throw new Exception("No printers were found");
}
}
请帮我.
lease Help me.
推荐答案
这篇关于如何获取网络上所有打印机的列表以检查它是联机还是脱机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!