如何获取网络上所有打印机的列表以检查它是联机还是脱机 [英] how to get a list of all printers on a network to check it is online or offline

查看:307
本文介绍了如何获取网络上所有打印机的列表以检查它是联机还是脱机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我如何获取网络上所有打印机的列表吗?
我尝试使用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.

推荐答案

没有一种方法可以获取所有打印机.这取决于您的打印机如何连接到网络.它们都可以通过单个打印服务器使用吗?他们在ActiveDirectory中吗?他们只是坐在IP地址旁,以便任何客户端安装自己的驱动程序并向其打印直接IP吗?
There''s no one method that''s going to get all printers. It depends on how your printers are connected to the network. Are they all available through a single print server? Are they in ActiveDirectory? Are they just sitting around with IP addresses so any client install their own drivers and print direct IP to them??


这篇关于如何获取网络上所有打印机的列表以检查它是联机还是脱机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆