使用ManagementObjectSearcher和Win32_Printer检查状态

时间:2016-12-07 20:20:48

标签: c# printing thermal-printer

我被迫使用RawPrinterHelper类打印到热敏打印机,因为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对不起我没问好。例如,空闲(3)表示可以打印。我想知道是否有更多也表明可以打印如此;打印(4),WarmUp(5),ect或空闲(3)是否应该发送下一个打印作业?感谢

2 个答案:

答案 0 :(得分:1)

以下是您可以考虑的值,以检查它是否已准备好打印:

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

另请注意关于Win32_Printer-

part文档的备注
  

如果要检索 PrinterStatus = 3 PrinterState = 0 ,则打印机驱动程序可能无法将准确信息提供给WMI。 WMI从 spoolsv.exe 进程检索打印机信息。打印机驱动程序可能不会向假脱机程序报告其状态。在这种情况下,Win32_Printer将打印机报告为空闲。

答案 1 :(得分:0)

**//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