Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
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 :( )
Posted
Updated 14-Feb-13 4:45am
v2
Comments
Mike Meinz 14-Feb-13 9:44am    
My Guess
My guess is that it takes the Print Spooler some time to process your big print file. Depending on the settings of your print driver, the printer may not be busy until the Print Spooler (1) is done spooling all of the print data OR (2) has completed some amount of processing on the print data. After one of those two events (depending on the printer's configuration), actual printing will begin and then the printer will be busy.
Alexandre Bencz 14-Feb-13 10:37am    
ok, sorry my incompetence, but, how I can make this ????? :X :(
Mike Meinz 14-Feb-13 16:25pm    
It looks like this is a common problem.
See this message thread.
Sergey Alexandrovich Kryukov 14-Feb-13 13:28pm    
I never tried that, but this is the model question showing how a question should be asked: explanation of what you want to achieve, complete but short sample code, steps to reproduce, explanation of what was expected and what's wrong.

Even the code incorrect from the standpoint of a working application (spin wait with repeated lala and the sloppy name of this method) is quite fine for a code sample used to reproduce the problem. I would note that such sample code is possible even for UI, all it needs is just not using the designer.

My 5 for the question. I'll bookmark it and will probably use to explain people how to ask question. Unfortunately, we are overwhelmed by non-questions and totally incorrect ones...
Thank you,
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



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