Retrieve a List of Installed Printers on the Network Using WMI, C# and VB.NET
Posted by: Suprotim Agarwal ,
on 4/30/2008,
in
Category WinForms & WinRT
Abstract: A common requirement is to display a list of printers on the network. In this article, we will explore how to do so. We will be using Windows Management Instrumentation(WMI) to access this information.WMI is like an interface for retrieving management information in an enterprise environment.
Retrieve a List of Installed Printers on the Network Using WMI, C# and VB.NET
A common requirement is to display a list of printers on the network. In this article, we will explore how to do so. We will be using Windows Management Instrumentation(WMI) to access this information.WMI is like an interface for retrieving management information in an enterprise environment.
To display the list of printers on your network, follow these steps:
Step 1: Create a Windows Form application and drag and drop a ComboBox(cmbPrinters) and a Button(btnGetPrinters) to the form.
Step 2: Right Click Project > Add Reference > Select .NET Tab > System.Management. Also add the following namespace in the Form1.cs
C#
using System.Management;
VB.NET
Imports System.Management
The System.Management namespace provides information and events about devices and applications instrumented to the WMI infrastructure.
Step 3: Double click the button to generate an event handler and add the following code:
C#
private void btnGetPrinters_Click(object sender, EventArgs e)
{
// Use the ObjectQuery to get the list of configured printers
System.Management.ObjectQuery oquery =
new System.Management.ObjectQuery("SELECT * FROM Win32_Printer");
System.Management.ManagementObjectSearcher mosearcher =
new System.Management.ManagementObjectSearcher(oquery);
System.Management.ManagementObjectCollection moc = mosearcher.Get();
foreach (ManagementObject mo in moc)
{
System.Management.PropertyDataCollection pdc = mo.Properties;
foreach (System.Management.PropertyData pd in pdc)
{
if ((bool)mo["Network"])
{
cmbPrinters.Items.Add(mo[pd.Name]);
}
}
}
}
VB.NET
Private Sub btnGetPrinters_Click(ByVal sender As Object, ByVal e As EventArgs)
' Use the ObjectQuery to get the list of configured printers
Dim oquery As System.Management.ObjectQuery = New System.Management.ObjectQuery("SELECT * FROM Win32_Printer")
Dim mosearcher As System.Management.ManagementObjectSearcher = New System.Management.ManagementObjectSearcher(oquery)
Dim moc As System.Management.ManagementObjectCollection = mosearcher.Get()
For Each mo As ManagementObject In moc
Dim pdc As System.Management.PropertyDataCollection = mo.Properties
For Each pd As System.Management.PropertyData In pdc
If CBool(mo("Network")) Then
cmbPrinters.Items.Add(mo(pd.Name))
End If
Next pd
Next mo
End Sub
In the code above, an instance of the ObjectQuery class is created to fetch a list of configured printers. This instance of the ObjectQuery is then passed to the constructor of the ManagementObjectSearcher class to retrieve a collection of management objects, in our case, printers. We then call the Get() method on the instance of the ManagementObjectSearcher class, to execute the query and return a list of printers to the instance of ManagementObjectCollection. The last step is to enumerate this collection and retrieve a set of properties of the WMI object. The ManagementObject is indexed with the propertyname to retrieve the name of the printer and is added to the ComboBox items.
Note: If you only want to get a list of printers installed on your local machine, you do not necessarily have to use WMI. It can be as simple as retrieving information using the System.Drawing.Printing.PrinterSettings.InstalledPrinters collection as shown below.
C#
foreach (string printer in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
{
MessageBox.Show(printer);
}
VB.NET
For Each printer As String In System.Drawing.Printing.PrinterSettings.InstalledPrinters
MessageBox.Show(printer)
Next printer
That’s it. If you are interested in delving deeper into System.Management, you can do so by checking this MSDN documentation. This namespace has several uses like finding out information such as CPU utilization, disk free space etc. I would encourage you to explore the various classes of this namespace.
I hope this article was useful and I thank you for viewing it.
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!
Was this article worth reading? Share it with fellow developers too. Thanks!
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of
DotNetCurry,
DNC Magazine for Developers,
SQLServerCurry and
DevCurry. He has also authored a couple of books
51 Recipes using jQuery with ASP.NET Controls and
The Absolutely Awesome jQuery CookBook.
Suprotim received the prestigious Microsoft MVP award for 17 consecutive years, until he resigned from the program in 2025. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.
Get in touch with him on Twitter @suprotimagarwal or at LinkedIn