This is a small console app that monitors a Windows printer's health using WMI (Win32_Printer) and prints current status.
Monitoring: Microsoft Print to PDF
Ready | State:Idle | Status:Idle | DetectedError:Unknown | DetectedErrorEx:Unknown
Ready | State:Idle | Status:Idle | DetectedError:Unknown | DetectedErrorEx:Unknown
...
Ready | State:17408 | Status:Printing | DetectedError:NoError | DetectedErrorEx:Unknown
Ready | State:Unk_Printing | Status:Printing | DetectedError:NoError | DetectedErrorEx:Unknown
Ready | State:Idle | Status:Idle | DetectedError:Unknown | DetectedErrorEx:Unknown
Ready | State:Idle | Status:Idle | DetectedError:Unknown | DetectedErrorEx:Unknown
...
Ready | State:Idle | Status:Idle | DetectedError:Unknown | DetectedErrorEx:Unknown
- Determines which printer to monitor (from command line or a default name).
- Every second:
- Queries WMI for that printer.
- Reads its state, status, and error flags.
- Decides if the printer is
OKorFAULTY. - Logs the result.
- Await
Task.Delay(1000)pauses for ~1 second between checks.
- Stops when you press a key.
To access WMI (Windows Management Instrumentation) the project using System.Management namespace methods. The program uses top-level statements (no explicit Main method).
Build and run on Windows with .NET, for example:
dotnet run "My Printer Name"or just:
dotnet runIf you don't specify a printer name in the arguments, it defaults to watching Microsoft Print to PDF. Find it in the following lines:
const string DefaultPrinterName = "Microsoft Print to PDF";DoHealthCheck(string nick) is where it talks to Windows:
It builds a WMI query for the printer:
string query = $"SELECT * from Win32_Printer WHERE Name LIKE '%{nick}%'";Uses ManagementObjectSearcher:
using ManagementObjectSearcher searcher = new(query);
using ManagementObjectCollection folks = searcher.Get();Then iterates over the printers returned:
- For each
buddyinfolks, it reads the printer'sDeviceIdvia the extension methodValueOrDefaultOf. - If
DeviceIdcontains the nickname (nick), that's considered the matching printer. - For that one, it calls
AreYouReady( buddy)to decide if it is OK or in fault.
After the loop, it calls:
GC.Collect();
GC.WaitForFullGCComplete(
TimeSpan.FromMilliseconds(100));which forces garbage collection and waits for a full GC. This is a bit aggressive, presumably to quickly free unmanaged resources related to WMI objects.