Click here to Skip to main content
15,893,486 members

Get the HP printer status

Alexandre Bencz asked:

Open original thread
I need to get the HP printer status, I have written this code:

using System;
using System.Collections;
using System.Linq;
using System.Management;
using System.Text;

namespace PrinterStatus
{
    class Program
    {
        private static int asd;
        private static string[] printerStatus = { "Other", "Unknown", "Idle", "Printing", "WarmUp", "Stopped Printing", "Offline" };
        private static string[] printerState = {"Paused","Error","Pending Deletion","Paper Jam","Paper Out","Manual Feed","Paper Problem", "Offline","IO Active","Busy","Printing",
            "Output Bin Full","Not Available","Waiting", "Processing","Initialization","Warming Up","Toner Low","No Toner","Page Punt", "User Intervention Required",
            "Out of Memory","Door Open","Server_Unknown","Power Save"};

        private static string printerName = "PDFPrt";

        static void lala()
        {
            string query = string.Format("SELECT * from Win32_Printer WHERE Name LIKE '%{0}'", printerName);
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
            ManagementObjectCollection coll = searcher.Get();

            foreach (ManagementObject printer in coll)
            {
                foreach (PropertyData property in printer.Properties)
                {
                    if (property.Name.Equals("ExtendedPrinterStatus"))
                    {
                        if (asd != Convert.ToInt32(property.Value))
                        {
                            Console.WriteLine(">>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<");
                            Console.ReadKey(true);
                        }
                        Console.WriteLine(string.Format("{0}: {1}", property.Name, printerStatus[Convert.ToInt32(property.Value)]));
                        asd = Convert.ToInt32(property.Value);
                    }
                }
            }

        }
        static void Main(string[] args)
        {
            while (true)
            {
                lala();
            }
        }
    }
}


By the way, it's just test code :) So, I opened this program, before I open the program I have put a big file to print, but, the return information of printer is always idle. I think the correct answer is printing or busy. How I can solve this? ( maybe it's a wrong code :( )
Tags: C#, Printing

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900