469,352 Members | 1,648 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

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

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

Nov 16 '05 #1
7 27527
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

"trint" <tr***********@gmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
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

Nov 16 '05 #2
In general the printer status can be queried using System.Management and
WMI.

Willy.

"trint" <tr***********@gmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
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

Nov 16 '05 #3

"trint" <tr***********@gmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
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.
Nov 16 '05 #4
I will try this...
Thanks,
Trint

Nov 16 '05 #5
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

Nov 16 '05 #6
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


trint wrote:
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


Nov 16 '05 #7
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.
"trint" <tr***********@gmail.com> wrote in message
news:11*********************@c13g2000cwb.googlegro ups.com...
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


trint wrote:
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

Nov 16 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Gordon Truslove | last post: by
3 posts views Thread by Gordon Truslove | last post: by
reply views Thread by cortukmehmet | last post: by
1 post views Thread by Vanessa | last post: by
6 posts views Thread by Pipo | last post: by
2 posts views Thread by TARUN | last post: by
9 posts views Thread by id10t error | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.