Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

PrinterStatus on local and network printers

370 views
Skip to first unread message

Anders Eriksson

unread,
Mar 29, 2004, 4:22:24 AM3/29/04
to
Hello!

I need to be able to get PrinterStatus for the default printer. The default
printer may be a local printer but it also may be a network printer.

To get the default printer name I use:

PrintDocument pd = new PrintDocument();
printerName = pd.PrinterSettings.PrinterName;

For a local printer I get e.g. "HP Laserjet 1100"
for a network printer I get e.g. "\\<ComputerName>\HP Laserjet 1100"

When I use this in the query:

oConn = new ConnectionOptions();
oMs = new System.Management.ManagementScope();

wmiq = "select * from Win32_Printer where Name ='" + printerName + "'";
oQuery = new System.Management.ObjectQuery(wmiq);
oSearcher = new ManagementObjectSearcher(oMs,oQuery);
oReturnCollection = oSearcher.Get();
foreach( ManagementObject oPrinter in oReturnCollection )
{
// check status on printer
}

On the local printer it seems to work, but on the network printer I get
"Invalid Query" on the foreach() line

What am i doing wrong?

How do I check the PrinterStatus on both local and network printers?
I nedd to be able to do this on win98, win2000, winXP.

I'm using C# and .Net 2003, Windows 2000

// Anders

gv

unread,
Mar 29, 2004, 11:05:24 AM3/29/04
to
The following c# will display the deviceID, printerstatus, and printerstate
for both local and network printers. I highly recommend downloading the WMI
tools from MS. When you run WMI CIM studio, you can search for your
namespace (here it is Win32_Printer), then you will see all the properties
available (like DeviceID, PrinterStatus, etc). Just substitute the property
in the square brackets below and it should display the value of that
property for that printers.

HTH
gv


private static void CheckPrinterStatus()
{
ManagementObjectSearcher query; //Object used to make the WMI query
ManagementObjectCollection queryCollection; //Object used to hold the
results of the WMI query (collection)
string queryString = "SELECT * FROM Win32_Printer";

try
{
query = new ManagementObjectSearcher(queryString);
queryCollection = query.Get();
//Loop thru the returned collection of printers
foreach( ManagementObject mo in queryCollection )
{
Console.WriteLine("DeviceID: " + (string)mo["DeviceID"]);
Console.WriteLine("PrinterStatus: " +
Convert.ToString(mo["PrinterStatus"]));
Console.WriteLine("PrinterState: " +
Convert.ToString(mo["PrinterState"]) + "\n");
}
}

catch(Exception e)
{
Console.WriteLine(e.Message);
}
}

"Anders Eriksson" <anders....@morateknikutveckling.se> wrote in message
news:owfwycep...@morateknikutveckling.se...

Andy Cheung [MSFT]

unread,
Mar 29, 2004, 10:04:13 PM3/29/04
to
The problem is caused by insufficient escaped character '\'. This is needed
as csc.exe will remove some of the escaped characters '\' before passing the
variable value to WMI.

Try
string printerName = "\\\\\\\\<ComputerName>\\\\HP LaserJet 1100";
Or
string printerName = @"\\\\<ComputerName>\\HP LaserJet 1100";

--
Andy Cheung
Microsoft WMI Test Engineer
This posting is provided "As Is" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm


"Anders Eriksson" <anders....@morateknikutveckling.se> wrote in message
news:owfwycep...@morateknikutveckling.se...

Anders Eriksson

unread,
Mar 30, 2004, 1:50:29 AM3/30/04
to
On Mon, 29 Mar 2004 19:04:13 -0800, Andy Cheung [MSFT] wrote:

> The problem is caused by insufficient escaped character '\'. This is needed
> as csc.exe will remove some of the escaped characters '\' before passing the
> variable value to WMI.
>
> Try
> string printerName = "\\\\\\\\<ComputerName>\\\\HP LaserJet 1100";
> Or
> string printerName = @"\\\\<ComputerName>\\HP LaserJet 1100";

But I haven't any literal!

I'm new at C# but in C++ I don't need to escape \ if I get it from a
function, in this case from

PrintDocument pd = new PrintDocument();
printerName = pd.PrinterSettings.PrinterName;

How do I change the printerName variagle so it escapes \?

// Anders

Anders Eriksson

unread,
Mar 30, 2004, 2:00:11 AM3/30/04
to
On Mon, 29 Mar 2004 11:05:24 -0500, <gv> wrote:

> The following c# will display the deviceID, printerstatus, and printerstate
> for both local and network printers.

Thank you for the code! Now I have gotten a bit further. I have another
problem but I will ask in a new thread.


>I highly recommend downloading the WMI
> tools from MS. When you run WMI CIM studio, you can search for your
> namespace (here it is Win32_Printer), then you will see all the properties
> available (like DeviceID, PrinterStatus, etc). Just substitute the property
> in the square brackets below and it should display the value of that
> property for that printers.
>

After bit of tinkering I have found out how to use CIM Studio and it is a
big help

Thank You!

// Anders

Andy Cheung [MSFT]

unread,
Mar 30, 2004, 1:15:57 PM3/30/04
to
I see. In this case, what WMI is expecting for the printerName in your WQL
query is \\\\<computerName>\\<printerName>. You can try the following code:

printerName = pd.PrinterSettings.PrinterName;
printerName = printerName.Replace("\\", "\\\\");

--
Andy Cheung
Microsoft WMI Test Engineer
This posting is provided "As Is" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm


"Anders Eriksson" <anders....@morateknikutveckling.se> wrote in message

news:l4k2aik3...@morateknikutveckling.se...

0 new messages