how to get printer status example like:printer offline and printer paper status

Posted by Raj.Net under WPF on 12/30/2013 | Points: 10 | Views : 7526 | Status : [Member] | Replies : 2
hi,
please help me guys i have used microsoft win32_printer property how to get printer paper status i am attached in sample code for your reference.
--------------------------------------------------------------------
//this method return for printer status
public static bool IsPrinterOffline()
{
string WorkOffline = "";
// Set management scope
ManagementScope scope = new ManagementScope("\\root\\cimv2");
scope.Connect();
// Select Printers from WMI Object Collections
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Printer");
string strDefaultPrinterName= GetDefaultPrinterName();
string strPrnName = "";

foreach (ManagementObject printer in searcher.Get())
{

strPrnName = printer["Name"].ToString().ToLower();

if (strPrnName.Equals(strDefaultPrinterName.ToLower()))
{
if (printer["WorkOffline"].ToString().ToLower().Equals("true"))
{
WritePrintstatusLog(WorkOffline);
return true;
}
else
{
// printer is not offline
return false;
}
}
}
return false;
}
---------------------------------------------------------------
//get default printer name
public static string GetDefaultPrinterName()
{
var query = new ObjectQuery("SELECT * FROM Win32_Printer");
var searcher = new ManagementObjectSearcher(query);

foreach (ManagementObject mo in searcher.Get())
{
if (((bool?)mo["Default"]) ?? false)
{
return mo["Name"] as string;
}
}

return null;
}

If it helps/directs you towards the solution,Mark This Response as Answer link
--
Rajesh



Responses

Posted by: Longwest on: 3/24/2015 [Member] Starter | Points: 25

Up
0
Down
https://support.microsoft.com/en-us/kb/160129


Best regards.
http://www.keepdynamic.com/

Raj.Net, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response