I am using this code to check if my printer is in the network. When I hit the Print button the first time, it gives me not connected status. And when I hit it for the second time it prints successfully. But my printer is always in the network.
public bool IsPrinterReady(string PrinterName)
{
ManagementScope scope = new ManagementScope(@"\root\cimv2");
scope.Connect();
ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_Printer");
string strPrinter = string.Empty;
foreach(ManagementObject printer in searcher.Get())
{
strPrinter = printer["Name"].ToString();
if(strPrinter.ToLower() == PrinterName.ToLower())
return printer["PrinterStatus"].ToString().ToLower().Equals("3");
}
return false;
}
Please note that I always get the value as true for my following if statement
if(strPrinter.ToLower() == PrinterName.ToLower())
I just get varied values from
printer["PrinterStatus"].ToString()
Thanks in advance