how to set Action when printer is connected

  • Thread starter Thread starter feiyim
  • Start date Start date
F

feiyim

Guest
Hi members!

Am wondering how i could fire off an even when the printer's status comes online. Am apparently running through WMI sample codes where i determine the status although its proving to be unreliable at times. Right now, i dont have any printers connected to my computer and some are reporting that they are online with an exception of Fax and OneNote.

Here is how am querying for the available printers.

var printerInfos = new List<PrinterInfo>();

try
{
var searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_Printer")
{
Options = {UseAmendedQualifiers = true}
};

printerInfos.AddRange(searcher.Get().OfType<ManagementObject>().Select(queryObj => new PrinterInfo {PrinterName = queryObj["Name"].ToString(), IsOnline = !bool.Parse(queryObj["WorkOffline"].ToString())}));
}
catch (Exception ex)
{
Send(JsonConvert.SerializeObject(ex.StackTrace, Formatting.Indented));
}

Send(JsonConvert.SerializeObject(printerInfos.Where(x => !x.PrinterName.Equals("Fax") && x.IsOnline == true), Formatting.Indented));


I then push them up an open websocket into a browser.


With all the available WMI examples, this only is the one i find to give more reliable data about the printer status (connected/not connected).


But then, i want to fire off an event to pass through the web socket, if connected, that the printer status is now online or offline. How do i achieve that?

Continue reading...
 
Back
Top