【问题标题】:How to detect whether a printer is connected or not?如何检测打印机是否连接?
【发布时间】:2019-07-11 00:30:39
【问题描述】:

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

【问题讨论】:

    标签: c# windows


    【解决方案1】:

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

    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;
                }
            }
          }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多