https://github.com/nikvoronin/howdy-mr-printer
How to check the printer status in Windows
https://github.com/nikvoronin/howdy-mr-printer
cim csharp dotnet printer printer-errors printer-monitor printer-status printers windows wmi
Last synced: about 1 month ago
JSON representation
How to check the printer status in Windows
- Host: GitHub
- URL: https://github.com/nikvoronin/howdy-mr-printer
- Owner: nikvoronin
- Created: 2022-02-02T19:40:35.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-02-02T20:45:06.000Z (over 4 years ago)
- Last Synced: 2025-01-16T11:27:05.165Z (over 1 year ago)
- Topics: cim, csharp, dotnet, printer, printer-errors, printer-monitor, printer-status, printers, windows, wmi
- Language: C#
- Homepage:
- Size: 8.79 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Howdy Mr Printer
This is a small console app that monitors a Windows printer's health using WMI (Win32_Printer) and prints current status.
```log
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 `OK` or `FAULTY`.
- 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
Build and run on Windows with .NET, for example:
```shell
dotnet run "My Printer Name"
```
or just:
```shell
dotnet run
```
If you don't specify a printer name in the arguments, it defaults to watching `Microsoft Print to PDF`. Find it in the following lines:
```csharp
const string DefaultPrinterName = "Microsoft Print to PDF";
```
## Querying WMI
`DoHealthCheck(string nick)` is where it talks to Windows:
It builds a WMI query for the printer:
```csharp
string query = $"SELECT * from Win32_Printer WHERE Name LIKE '%{nick}%'";
```
Uses ManagementObjectSearcher:
```csharp
using ManagementObjectSearcher searcher = new(query);
using ManagementObjectCollection folks = searcher.Get();
```
Then iterates over the printers returned:
- For each `buddy` in `folks`, it reads the printer's `DeviceId` via the extension method `ValueOrDefaultOf`.
- If `DeviceId` contains 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:
```csharp
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.