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 :( )