使用 ManagementObjectSearcher 和 Win32_Printer 检查状态

Using ManagementObjectSearcher and Win32_Printer to check Status

我被迫使用 RawPrinterHelper class 打印到热敏打印机,因为 PrintDocument 方法已被证明不适合 POS 打印。

我现在需要在打印之前检查打印机的状态,以确保它在线并准备好打印。我已经能够成功检查 Win32_Printer 的属性。当缺纸或托盘打开时,我可以看到 PrinterStatus 等属性从 3 变为 2 或 1。这太棒了。

我的问题是,哪些属性应该表明它是O.K。打印?肯定不仅仅是检查 PrinterStatus 是否空闲 (3).

    private bool ReadyCheck(string printerName)
    {
        bool ready = false;
        string query = string.Format("SELECT * from Win32_Printer WHERE Name LIKE '%{0}'", printerName);

        using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
        using (ManagementObjectCollection coll = searcher.Get())
        {
            try
            {
                foreach (ManagementObject printer in coll)
                {
                    // Print status = 3, idle and ready to print
                    string status = printer.Properties["PrinterStatus"].Value.ToString();
                    string extendedPrinterStatus = printer.Properties["ExtendedPrinterStatus"].Value.ToString();

                    // What else means printer is ready?
                    if (status.Trim() == "3")
                        ready = true;

                }
            }
            catch (ManagementException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        return ready;

    }

编辑: @vendettamit 对不起,我问得不是很好。例如,Idle (3) 表示可以打印。我想知道是否还有更多表明可以打印的信息,例如;正在打印 (4)、预热 (5) 等,或者空闲 (3) 是您应该发送下一个打印作业的唯一时间吗?谢谢

以下是您在检查是否可以打印时可以考虑的值:

Other (1)
Unknown (2)
Idle (3) 
Printing (4)
Warmup (5)
Stopped Printing (6)
Offline (7)

Win32_Printer-

MSDN 文档的评论中的备注

If you are retrieving PrinterStatus = 3 or PrinterState = 0, the printer driver may not be feeding accurate information into WMI. WMI retrieves the printer information from the spoolsv.exe process. It is possible the printer driver does not report its status to the spooler. In this case, Win32_Printer reports the printer as Idle.

**//check printer is online**
private static bool IsOnline(ManagementBaseObject printer)
        {
            bool isOnlineprinter = true;
            PrinterNative.PrinterNative.PrinterNative printerNative = new PrinterNative.PrinterNative.PrinterNative();
            var PrinterName = printerNative.GetPrinterName();
            var PrinterNameProperty = printer.Properties["DeviceId"].Value.ToString();
            var ResultPrinter01 = printer.Properties["ExtendedPrinterStatus"].Value.ToString();
            var ResultPrinter02 = printer.Properties["PrinterState"].Value.ToString();

            if (PrinterNameProperty == PrinterName)
            {
                //(no internet connection or printer switched off):PrinterState
                if (ResultPrinter02 == "128"|| ResultPrinter02=="4096")
                {
                    isOnlineprinter = false;
                }



                ////printer is initializing....
                //if (ResultPrinter02 == "16")
                //{
                //    isOnlineprinter = false;
                //}

                //(no internet connection or printer switched off):ExtendedPrinterStatus
                if (ResultPrinter01 == "7")
                {
                    isOnlineprinter = false;
                }

            }

            return isOnlineprinter;
         }
         
         
         
         
  **//check for out of paper**
private static bool IspaperOK(ManagementBaseObject printer)
        {
            bool PaperOK = true;
            
            PrinterNative.PrinterNative.PrinterNative printerNative = new PrinterNative.PrinterNative.PrinterNative();
            var PrinterName = printerNative.GetPrinterName();
            var PrinterNameProperty = printer.Properties["DeviceId"].Value.ToString();

            var PaperStatus = printer.Properties["PrinterState"].Value.ToString();
            
            if (PrinterNameProperty == PrinterName)
            {
                //(PrinterState)16 = Out of Paper
                //(PrinterState)5 = Out of paper
                //(PrinterState)4 = paperjam
                //(PrinterState)144 = Out of paper
                if ((PaperStatus == "5") || (PaperStatus == "16")||(PaperStatus=="144"))
                {
                    PaperOK = false;
                }

            }

            return PaperOK;
        }
        
        
        
**//Verify still printing state or not**
private static bool Isprinting(ManagementBaseObject printer)
        {
            bool Isprintingnow = false;
            PrinterNative.PrinterNative.PrinterNative printerNative = new PrinterNative.PrinterNative.PrinterNative();
            var PrinterName = printerNative.GetPrinterName();
            var PrinterNameProperty = printer.Properties["DeviceId"].Value.ToString();
            var printing01 = printer.Properties["PrinterState"].Value.ToString();
            var printing02 = printer.Properties["PrinterStatus"].Value.ToString();
            if (PrinterNameProperty == PrinterName)
            {
                //(PrinterState)11 = Printing
                //(PrinterState)1024 = printing
                //(PrinterStatus)4 = printing
                if (printing01 == "11" || printing01 == "1024" || printing02=="4")
                {
                    Isprintingnow = true;
                }

            }

            return Isprintingnow;
        }

**//check for error (Printer)**
 private static bool IsPrinterError(ManagementBaseObject printer)
        {
            bool PrinterOK = true;

            PrinterNative.PrinterNative.PrinterNative printerNative = new PrinterNative.PrinterNative.PrinterNative();
            var PrinterName = printerNative.GetPrinterName();
            var PrinterNameProperty = printer.Properties["DeviceId"].Value.ToString();

            var PrinterSpecificError = printer.Properties["PrinterState"].Value.ToString();
            var otherError = printer.Properties["ExtendedPrinterStatus"].Value.ToString();
            if (PrinterNameProperty == PrinterName)
            {
                //(PrinterState)2 - error of printer
                //(PrinterState)131072 - Toner Low
                //(PrinterState)18 - Toner Low
                //(PrinterState)19 - No Toner

                if ((PrinterSpecificError == "131072")||(PrinterSpecificError == "18")||(PrinterSpecificError == "19")||(PrinterSpecificError == "2")||(PrinterSpecificError == "7"))
                {
                    PrinterOK = false;
                }

                //(ExtendedPrinterStatus) 2 - no error
                if (otherError=="2")
                {
                    PrinterOK = true;
                }
                else
                {
                    PrinterOK = false;
                }

            }
            return PrinterOK;
        }

    **//check Network or USB**
private static bool IsNetworkPrinter(ManagementBaseObject printer)
    {
        bool IsNetwork = true;
        PrinterNative.PrinterNative.PrinterNative printerNative = new PrinterNative.PrinterNative.PrinterNative();
        var PrinterName = printerNative.GetPrinterName();
        var PrinterNameProperty = printer.Properties["DeviceId"].Value.ToString();
      
        var network = printer.Properties["Network"].Value.ToString();                   
        var local = printer.Properties["Local"].Value.ToString();

                    if (PrinterNameProperty == PrinterName)
                    {
                        if (network == "True")
                        {
                            IsNetwork = true;
                        }

                        if (network == "True" && local == "True")
                        {
                            IsNetwork = true;
                        }

                        if (local == "True" && network=="False")
                        {
                            IsNetwork = false;
                        }
                    }

        return IsNetwork;
    }

         

            //(PrinterState)16 = Out of Paper
            //(PrinterState)5 = Out of paper
            //(PrinterState)4 = paperjam
            //(PrinterState)144 = Out of paper
            //(PrinterState)4194432 = Lid Open        
            //(PrinterState)4194448 = Out of paper/Lid open 
            //(PrinterState)4096= Offline   
            //(PrinterState)1024= Printing
            //(PrinterState)128= Printer is offline