JonJames
JonJames

Reputation: 87

C# Trying to get the printer status when it is offline

I need to get the printer status when it is offline, below is how I am getting the status;

 // Set management scope
                ManagementScope scope = new ManagementScope(@"\root\cimv2");
                scope.Connect();

                // Select Printers from WMI Object Collections
                ManagementObjectSearcher searcher = new
                 ManagementObjectSearcher("SELECT * FROM Win32_Printer");

                string printerName = "";
                foreach (ManagementObject printer in searcher.Get())
                {
                    printerName = printer["Name"].ToString();
                    if (printerName.Equals(@"TEC B-EV4-T"))
                    {
                        //Console.WriteLine("Printer = " + printer["Name"]);
                        if (printer.Properties["PrinterStatus"].Value.ToString() == "7")
                        {
                            // printer is offline by user
                            MessageBox.Show("TEC B-EV4-T is offline");
                        }
                        else
                        {
                            // printer is not offline 
                            //Check API first then print
                            ReportDocument cryRpt = new ReportDocument();
                            cryRpt.Load(Environment.CurrentDirectory + "\\Report.rpt");

                            dialog.PrintQueue = new PrintQueue(new PrintServer(), "TEC B-EV4-T");

                            //to the data table here
                            //To data table
                            DataTable dt = ToDataTable(lotinstruct);

                            cryRpt.SetDataSource(dt);

                            cryRpt.PrintToPrinter(1, true, 0, 0);

                            //}

                            MessageBox.Show("Already save result please check label!");
                        }
                    }
                }

The problem is here if (printer.Properties["PrinterStatus"].Value.ToString() == "7") it seems like the printer status is always 3 when I debug (printer status 3 means that it is idle 7 means that it is offline). Is there any way to find out if the printer TEC B-EV4-T is offline?

Upvotes: 2

Views: 5194

Answers (2)

Zoltan
Zoltan

Reputation: 167

Don't use printer["PrinterStatus"]. You have to use printer["WorkOffline"] like below:

        bool IsOffline = false;
        ManagementObjectSearcher searcher = new
        ManagementObjectSearcher("SELECT * FROM Win32_Printer where Name='" + YourPrinter + "'");
        foreach(ManagementObject printer in searcher.Get())
        {
            IsOffline = (bool)printer["WorkOffline"];
        }
        if (IsOffline)
        {
         ...
        }

Upvotes: 3

Chirag Rupani
Chirag Rupani

Reputation: 1715

You can refer PrintQueue in built in .Net framework to check printer status. There are many properties in it to check different status:

string printerName = "TEC B-EV4-T";
var server = new LocalPrintServer();
PrintQueue queue = server.GetPrintQueue(printerName , Array.Empty<string>()); 
bool isOffline = queue.IsOffline;
bool isBusy = queue.IsBusy;

Upvotes: 1

Related Questions