Not possible to get printer status with c#.net?

  • Thread starter Thread starter trint
  • Start date Start date
T

trint

I am told by an experienced c#.net programmer that it is not possible
to get the code to get a printers status. I've been hammering on this
for a week and have combed through hp's web sites and google and
microsoft...nothing for c#. If anyone has a solution, I would
appreciate knowing.
Thanks,
Trint
 
This is a function for a printer driver. When you print to a spooler it is
not relevant whether the printer hardware is ready or not.

If you are bypassing the spooler and going direct to hardware for some
reason, then you are probably talking about a Win32 API call that you would
make from C# via P/Invoke.

--Bob
 
In general the printer status can be queried using System.Management and
WMI.

Willy.
 
trint said:
I am told by an experienced c#.net programmer that it is not possible
to get the code to get a printers status. I've been hammering on this
for a week and have combed through hp's web sites and google and
microsoft...nothing for c#. If anyone has a solution, I would
appreciate knowing.
Thanks,
Trint
Here is how:

using System;
using System.Management;


enum PrinterStatus {
Other = 1,
Unknown,
Idle,
Printing,
Warmup,
Stopped,
printing,
Offline
}

public class Wmis {
public static void Main() {
PrinterStatus stat;
if ((stat = GetPrinterStat("\\\\server\\printername")) != 0) // UNC or a
local name
{
Console.WriteLine(stat);
}
else
Console.WriteLine("Failed to get status");
}
static PrinterStatus GetPrinterStat(string printerDevice)
{
PrinterStatus ret = 0;
string path = "win32_printer.DeviceId='" + printerDevice + "'";
using (ManagementObject printer = new ManagementObject(path))
{
printer.Get();
PropertyDataCollection printerProperties = printer.Properties;
PrinterStatus st =
(PrinterStatus)Convert.ToInt32(printer.Properties["PrinterStatus"].Value);
ret = st;
}
return ret;
}
}


Willy.
 
Willy,
How can I make listBox1 visible? I changed your code slightly from:
if ((stat = GetPrinterStat("\\\\server\\printername")) != 0)
{
Console.WriteLine(stat);
}
To:
{
Form1.listBox1.Items.Add(stat);
}
so that I can get the error messages back in windows, but I get this
error message at compile:
'GetPrinterStatus1.Form1.listBox1' is inaccessible due to its
protection level
How do I change the listBox's protection level?
Thanks,
Trint
 
Ok,
Here is the fix to make it work in Windows:

public class Form1 : System.Windows.Forms.Form
{
....
protected System.Windows.Forms.ListBox listBox1;
....
....
public class Wmis: Form1
{
....
....
Wmis f1 = new Wmis();
f1.listBox1.Items.Add(stat);

The only thing is, I wish that I could get error codes like 'tray 2
empty' or 'tray 3 empty' or 'out of toner'.
Any suggestions?
Thanks,
Trint
 
Check the WMI info in MSDN. The class to search for is win32_printer.
Some printer manufactures supply printer drivers that fill the
ExtendedPrinterStatus, DetectErrorState and ExtendedDetectErrorState
property, some don't care at all about WMI.

Willy.
 
Back
Top