Skip to content

nikvoronin/howdy-mr-printer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Howdy Mr Printer

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 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:

dotnet run "My Printer Name"

or just:

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:

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:

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 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:

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.

About

Console app that monitors a Windows printer's health using WMI and prints current status

Topics

Resources

Stars

Watchers

Forks

Languages