public bool IsPrinterOnline(string printerName)
{
string str = "";
bool online = false;
//set the scope of this search to the local machine
ManagementScope scope = new ManagementScope(ManagementPath.DefaultPath);
//connect to the machine
scope.Connect();
//query for the ManagementObjectSearcher
SelectQuery query = new SelectQuery("select * from Win32_Printer");
ManagementClass m = new ManagementClass("Win32_Printer");
ManagementObjectSearcher obj = new ManagementObjectSearcher(scope, query);
//get each instance from the ManagementObjectSearcher object
using (ManagementObjectCollection printers = m.GetInstances())
//now loop through each printer instance returned
foreach (ManagementObject printer in printers)
{
//first make sure we got something back
if (printer != null)
{
//get the current printer name in the loop
str = printer["Name"].ToString().ToLower();
//check if it matches the name provided
if (str.Equals(printerName.ToLower()))
{
//since we found a match check it's status
if (printer["WorkOffline"].ToString().ToLower().Equals("true") || printer["PrinterStatus"].Equals(7))
//it's offline
online = false;
else
//it's online
online = true;
}
}
else
throw new Exception("No printers were found");
}
return online;
}
Взято с http://www.dreamincode.net
0.00 (0%) 0 votes




