如何检测打印机是否已连接? [英] How to detect whether a printer is connected or not?

查看:108
本文介绍了如何检测打印机是否已连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检测我的电脑是否安装了打印机以及打印机连接是否处于活动状态?

How to detect the whether a printer is installed in my PC or not and whether the printer connection is active or not?

推荐答案

该类将列出所有已安装的打印机并为您提供打印机的状态.

This class will list all the printer installed and give you the status of the printer.

using System;
using System.Management;

public class MyClass
{

      static void printProps(ManagementObject o,string prop){
            try{Console.WriteLine(prop+"|"+o[prop]);}catch(Exception e){Console.Write(e.ToString());}
      }

      [STAThread]
      static void Main(string[] args) 
      {
        ManagementObjectSearcher searcher = new 
        ManagementObjectSearcher("SELECT * FROM Win32_Printer where Default=True");

        string printerName = "";
        foreach (ManagementObject printer in searcher.Get()){
          printerName = printer["Name"].ToString().ToLower();
            Console.WriteLine("Printer :"+printerName);
            printProps(printer, "WorkOffline");
            //Console.WriteLine();
            switch( Int32.Parse( printer["PrinterStatus"].ToString() )){
                  case 1: Console.WriteLine("Other"); break;
                  case 2: Console.WriteLine("Unknown");break;
                  case 3: Console.WriteLine("Idle"); break;
                  case 4: Console.WriteLine("Printing"); break;
                  case 5: Console.WriteLine("Warmup"); break;
                  case 6: Console.WriteLine("Stopped printing"); break;
                  case 7: Console.WriteLine("Offline"); break;
            }
        }
      }
}

这篇关于如何检测打印机是否已连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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