Join 150,198 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 2,015 people online right now. Registration is fast and FREE... Join Now!
I have mini project which is getting Printer Status. Right now, i'm able to get simple printer status (online or offline). But i can't get further information of the printer such as Low Paper, Low Toner, Out of Paper, etc.
I have tried 2 properties ("Name" and "WorkOffline") to detect Name and Status of the printer. But, the other properties don't work on me (especially "DetectedErrorState" , "ExtendedDetectedErrorState"). I need that properties in order to get information about Paper and Toner problem.
namespace GetPrinterStatus { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void btnDetect_Click(object sender, EventArgs e) { string printerName = "CUSTOM VKP80 II"; // this is my printer name memoPrintDetail.Clear(); //memoPrintDetail is richTextBox to show my complete printer status later on
memoPrintDetail.AppendText("Printer Name : " + printerName + "\n");
ManagementScope scope = new ManagementScope(@"\root\cimv2"); scope.Connect();
// Select All Printers from my computer ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Printer"); ManagementObjectCollection collection = searcher.Get();
foreach (ManagementObject printer in collection) { if (printer["Name"].ToString() == printerName) { if (printer["WorkOffline"].ToString().ToLower().Equals("true")) // This code is working { memoPrintDetail.AppendText("Status : Offline\n");// This code is working return; } else { memoPrintDetail.AppendText("Status : Online\n");// This code is working } MessageBox.Show(printer["ExtendedDetectedErrorState"].ToString()); // Always show "0" value MessageBox.Show(printer["DetectedErrorState"].ToString()); // Always show "0" value } } } } }
Would u guys please help me, Why the last 2 codes always show "0" value ? I have tried put on the paper, remove the paper, put on the toner, and remove toner, But always get "0" value. What's wrong with my codes ? Does my printer support that feature ?
Below code seems to be working for me to get DetectedErrorState.
csharp
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_Printer"); foreach (ManagementObject queryObj in searcher.Get()) { Console.WriteLine("DetectedErrorState: {0}", queryObj["DetectedErrorState"]); }
And in case of ExtendedDetectedErrorState you need to keep one thing in mind that
QUOTE
In Windows 2000 and Windows NT 4.0, This property is not supported. -- MSDN
And again this seems working smooth...
csharp
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2","SELECT * FROM Win32_Printer"); foreach (ManagementObject queryObj in searcher.Get()) { Console.WriteLine("ExtendedDetectedErrorState: {0}", queryObj["ExtendedDetectedErrorState"]); }
What I felt like that PrinterStatus property will give the same results what you are looking for like Low Paper, Low Toner, Out of Paper. PrinterStatus seems to be working more efficent than DetectedErrorState and ExtendedDetectedErrorState.
csharp
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_Printer"); foreach (ManagementObject queryObj in searcher.Get()) { Console.WriteLine("PrinterStatus: {0}", queryObj["PrinterStatus"]); }
Here is another method to get the status and state of the printer (status includes the items you are looking for as well)
csharp
public static Hashtable GetPrinterProperties() { Hashtable properties = new Hashtable(); ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Printer");
//now loop thorugh all the objects in the searcher foreach (ManagementObject obj in searcher.Get()) { string[] printerStatus = { "Other", "Unknown", "Idle", "Printing", "WarmUp", "Stopped Printing", "Offline" }; 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"}; //now loop through all the properties foreach (PropertyData data in obj.Properties) { //make sure we have the default printer if ((bool)obj["Default"]) { switch (data.Name.ToLower()) { case "printerstate": properties.Add("State", printerState[Convert.ToInt32(data.Value)]); break; case "printerstatus": properties.Add("Status", printerStatus[Convert.ToInt32(data.Value)]); break; } } }
Here is another method to get the status and state of the printer (status includes the items you are looking for as well)
csharp
public static Hashtable GetPrinterProperties() { Hashtable properties = new Hashtable(); ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Printer");
//now loop thorugh all the objects in the searcher foreach (ManagementObject obj in searcher.Get()) { string[] printerStatus = { "Other", "Unknown", "Idle", "Printing", "WarmUp", "Stopped Printing", "Offline" }; 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"}; //now loop through all the properties foreach (PropertyData data in obj.Properties) { //make sure we have the default printer if ((bool)obj["Default"]) { switch (data.Name.ToLower()) { case "printerstate": properties.Add("State", printerState[Convert.ToInt32(data.Value)]); break; case "printerstatus": properties.Add("Status", printerStatus[Convert.ToInt32(data.Value)]); break; } } }
return properties; } }
Hi.. PsychoCoder, Thanks for your reply. I have tried your code in my project, but I still have some problems. The "PrinterStatus" always give me "3" ("Printing" Status) value even if the printer is turned off (should be "Offline" Status). I have problem with "PrinterState" too. It always give me "0" ("Paused" state) every single time.
I have tested on many printer which is connected to my computer. And the feedback is always the same even if there is a paper, no paper, offline printer, etc.
I have modified your code a little. I'm using Procedure instead of Function. This is what i have.
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Printer");
//now loop thorugh all the objects in the searcher foreach (ManagementObject obj in searcher.Get()) { if ((bool)obj["Default"]) { //now loop through all the properties foreach (PropertyData data in obj.Properties) { switch (data.Name.ToLower()) { case "name": memoPrintDetail.AppendText("Default Printer Name : " + data.Value + "\n"); //This Code is Working statComplete += 1; break; case "printerstate": memoPrintDetail.AppendText("Printer State : " + printerState[Convert.ToInt32(data.Value)] + "\n"); //Always give "Paused" state statComplete += 1; break; case "printerstatus": memoPrintDetail.AppendText("Printer Status : " + printerStatus[Convert.ToInt32(data.Value)] + "\n");//Always give "Printing" status statComplete += 1; break; } } } if (statComplete == 3) break; } }
What's wrong with my Code ??
This post has been edited by basslover: 17 Sep, 2008 - 05:43 PM