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
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...
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...
> 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
> 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
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...