Managing printers programmatically is a common requirement in enterprise environments where large-scale deployment and automation are necessary. Windows Management Instrumentation (WMI) provides a powerful interface to access and manipulate system hardware and configuration, including printers.
In this post, we'll explore managing printers in C# using WMI (System.Management namespace).
Managing Printers in C# using WMI
Getting Started
Windows Management Instrumentation (WMI) is an extension of the Windows Driver Model that offers an operating system interface, enabling components to provide information and notifications. It allows users to query and manage system elements such as printers, services, and processes.
Here in the post I am going to share code examples where you will get ideas to manage printers using the Windows extension that is WMI in C# which will help you to build a printer management application that can list, set default, install, or remove printers.
This example includes:- Listing All Installed Printers
- GetPrinterInfo
- Checking Printer Status
- Setting the Default Printer
- Installing a New Printer
- Deleting a Printer
System.Management.dll
.
Listing All Installed Printers
C# Code using System;
using System.Management;
class Program
{
static void Main()
{
Console.WriteLine("Installed Printers:");
// Create a WMI query for installed printers
string query = "SELECT * FROM Win32_Printer";
// Execute the query using ManagementObjectSearcher
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
{
foreach (ManagementObject printer in searcher.Get())
{
// Display printer name and other properties if needed
Console.WriteLine("Name: " + printer["Name"]);
Console.WriteLine(" Default: " + printer["Default"]);
Console.WriteLine(" Network Printer: " + printer["Network"]);
Console.WriteLine(" Status: " + printer["PrinterStatus"]);
Console.WriteLine();
}
}
}
}
Sample Output
Installed Printers:
Name: Microsoft Print to PDF
Default: False
Network Printer: False
Status: 3
Name: HP SMART TANK 520
Default: True
Network Printer: False
Status: 3
Get A Printer Infomation
Sample Output using System;
using System.Management;
class PrinterInfo
{
public static void GetPrinterInfo(string printerName)
{
string query = $"SELECT * FROM Win32_Printer WHERE Name = '{printerName.Replace("\\", "\\\\")}'";
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
ManagementObjectCollection printers = searcher.Get();
foreach (ManagementObject printer in printers)
{
Console.WriteLine("Printer Name: " + printer["Name"]);
Console.WriteLine(" Default: " + printer["Default"]);
Console.WriteLine(" Status: " + printer["Status"]);
Console.WriteLine(" Work Offline: " + printer["WorkOffline"]);
Console.WriteLine(" Network Printer: " + printer["Network"]);
Console.WriteLine(" Share Name: " + printer["ShareName"]);
Console.WriteLine(" Port Name: " + printer["PortName"]);
Console.WriteLine(" Driver Name: " + printer["DriverName"]);
Console.WriteLine(" Location: " + printer["Location"]);
Console.WriteLine(" Comment: " + printer["Comment"]);
Console.WriteLine(" Availability: " + printer["Availability"]);
}
if (printers.Count == 0)
{
Console.WriteLine("Printer not found.");
}
}
static void Main(string[] args)
{
// Replace with the actual name of your printer
string printerName = "Your Printer Name";
GetPrinterInfo(printerName);
}
}
Checking Printer Status
Sample Output using System;
using System.Management;
class PrinterStatusChecker
{
static void Main()
{
// Create a ManagementObjectSearcher to query printer info
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Printer");
foreach (ManagementObject printer in searcher.Get())
{
Console.WriteLine("Printer Name: " + printer["Name"]);
Console.WriteLine("Printer Status: " + printer["PrinterStatus"]);
Console.WriteLine("Work Offline: " + printer["WorkOffline"]);
Console.WriteLine("Is Default: " + printer["Default"]);
Console.WriteLine("Status: " + printer["Status"]);
Console.WriteLine("----------------------------------------");
}
}
}
Setting the Default Printer
Sample Output using System;
using System.Management;
class Program
{
static void Main(string[] args)
{
string printerName = "Your Printer Name Here";
try
{
// Query for the specific printer
string query = $"SELECT * FROM Win32_Printer WHERE Name = '{printerName.Replace("\\", "\\\\")}'";
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
{
foreach (ManagementObject printer in searcher.Get())
{
// Set it as default
printer.InvokeMethod("SetDefaultPrinter", null);
Console.WriteLine($"Printer '{printerName}' has been set as default.");
return;
}
Console.WriteLine($"Printer '{printerName}' not found.");
}
}
catch (Exception ex)
{
Console.WriteLine("Error setting default printer: " + ex.Message);
}
}
}
Installing a New Printer
To install a new printer using C# and WMI (Windows Management Instrumentation), you can utilize the ManagementClass and ManagementObject classes from the System.Management namespace. WMI can be used to manage printers, but it's important to note that WMI doesn't directly install printer drivers or printers; rather, it can be used to create and configure printer connections if the driver is already installed. Here’s C# example of how to add a network printer using WMI.
Sample Output
using System;
using System.Management;
class PrinterInstaller
{
static void Main()
{
string printerName = "My New Printer";
string portName = "IP_192.168.1.100"; // Ensure this port already exists
string driverName = "HP Universal Printing PCL 6"; // Must be pre-installed
try
{
ManagementClass printerClass = new ManagementClass("Win32_Printer");
ManagementBaseObject printerObj = printerClass.CreateInstance();
printerObj["Name"] = printerName;
printerObj["PortName"] = portName;
printerObj["DriverName"] = driverName;
printerObj["DeviceID"] = printerName;
printerObj["Location"] = "Office";
printerObj["Network"] = true;
printerObj["Shared"] = false;
printerObj["Default"] = false;
ManagementBaseObject result = printerObj.InvokeMethod("AddPrinterConnection", null, null);
// Alternative: Use Put() to create it
printerObj.Put();
Console.WriteLine("Printer installed successfully.");
}
catch (Exception ex)
{
Console.WriteLine("Failed to install printer: " + ex.Message);
}
}
}
Alternative
System.Diagnostics.Process.Start("rundll32.exe",
"printui.dll,PrintUIEntry /if /b \"My Printer\" /f \"C:\\Drivers\\hp.inf\" /r \"IP_192.168.1.100\" /m \"HP Universal Printing PCL 6\"");
Note:-
- The driver must already be installed on the system. You can pre-install it via command line using
pnputil
or other tools. - This script only sets up a local printer and port. It doesn't handle printer sharing or installing drivers.
- You can use
EnumPrinterDrivers
(via P/Invoke) to check installed drivers. - To install a shared network printer, use
Win32_Printer
with["Network"] = true
and set["ShareName"]
.
Deleting a Printer
Sample Output using System;
using System.Management;
class DeletePrinter
{
static void Main()
{
string printerName = "My New Printer"; // Replace with your printer's name
try
{
string query = $"SELECT * FROM Win32_Printer WHERE Name = '{printerName.Replace("\\", "\\\\")}'";
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
{
foreach (ManagementObject printer in searcher.Get())
{
printer.Delete();
Console.WriteLine($"Printer '{printerName}' deleted successfully.");
return;
}
}
Console.WriteLine($"Printer '{printerName}' not found.");
}
catch (Exception ex)
{
Console.WriteLine("Error deleting printer: " + ex.Message);
}
}
}
Summary
Using WMI in C# gives you fine-grained control over printer management and is useful in administrative or deployment tools. However, WMI can be verbose and sometimes challenging to debug, so ensure proper logging and error handling. For more advanced scenarios, like printer driver installation or network printer mapping, you may need to interface with PowerShell or Windows APIs directly.
Thanks