hi,
I'm trying to write a program that will, in real-time, look at the status of all the printers installed on a machine, and if certain ones are seen, then perform an action.
I am approaching this step by step, so I am currently trying to get a list of all the installed printers, and their current status.
I can get the details using printerQuery, but every printer is listed as unknown .
public static void GetWindowsPrinters(RichTextBox RTBName)
{
var printerQuery = new ManagementObjectSearcher("SELECT * from Win32_Printer");
foreach (var printer in printerQuery.Get())
{
var _name = printer.GetPropertyValue("Name");
var _status = printer.GetPropertyValue("Status");
var _isDefault = printer.GetPropertyValue("Default");
var _isNetworkPrinter = printer.GetPropertyValue("Network");
var _portName = printer.Properties["PortName"].Value;
RTBName.AppendText(_name + " -- Status: " + _status + ", Default: " + _isDefault + ", Port: " + _portName + Environment.NewLine + " --------- " + Environment.NewLine);
if (_isNetworkPrinter.ToString() == "true")
{
var searcher2 = new ManagementObjectSearcher("SELECT * FROM Win32_TCPIPPrinterPort where Name LIKE '" + _portName + "'");
var results2 = searcher2.Get();
RTBName.AppendText(Environment.NewLine + "PortName:" + _portName + " PortNumber:" + results2.ToString() + Environment.NewLine + Environment.NewLine);
}
}
}
Could someone point out where I am going wrong? why is everything coming back unknown ? I have 1 printer that windows 10 says are "not connected" (correct) and one that says it's "Not Available" (again, correct as it's off), but the Microsoft XPS document writer, FAX, Onenote etc are all "online".
I thought this was just me coding wrongly, so I tried to skip a few steps and use a watcher on the printers, but this fails also.
public void listprinters()
{
string wqlQuery = @"SELECT * from Win32_Printer";
WqlEventQuery query = new WqlEventQuery(wqlQuery);
watcher = new ManagementEventWatcher(query);
watcher.EventArrived +=
new EventArrivedEventHandler(onEvent);
watcher.Start();
}
public void onEvent(object sender, EventArrivedEventArgs e)
{
MessageBox.Show("Caught the Status" +"Printer Status");
watcher.Stop();
}
but when I invoke this, I get an unhandled exception @ the watcher.start line: - System.Management.ManagementException: 'Class is not an event class. '
Any help is always gratefully received.
thank you in advance