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
Views: 94034
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.
If you liked the article,  Subscribe to my RSS Feed.
What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author
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 a new one recently at The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigious Microsoft MVP award for nine times in a row now. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that represents premium web sites and digital publications comprising of Professional web, windows, mobile and cloud developers, technical managers, and architects.

Get in touch with him on Twitter @suprotimagarwal, LinkedIn or befriend him on Facebook



Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by Swetha on Monday, July 21, 2008 12:40 AM
Hi,
I need to get the list of printers from my network. No matter wherther it is installed or not.
Can you please help me in achieving this.
Comment posted by Suprotim Agarwal on Tuesday, October 7, 2008 11:00 PM
The code displayed above should get you the list of printers on the network. Alternatively, check out Xun-Ye's suggestion over here:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3916498&SiteID=1
Comment posted by joeyg on Wednesday, October 22, 2008 11:53 AM
Does not work for a list -- code attempts to check every property of the class.  Had to rewrite parts and still, the Win32_Printers only lists LOCALLY defined printers.
Comment posted by Suprotim Agarwal on Wednesday, October 22, 2008 10:28 PM
joeyg: Did you check out Xun-Ye's suggested marked in the earlier comment?
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3916498&SiteID=1
Comment posted by Michelle on Thursday, October 3, 2013 10:49 AM
I am trying to pull a list of computers from AD then from that list get the default printer that is installed.  I don't know how to combine the AD search with a WMI.

Categories

JOIN OUR COMMUNITY

POPULAR ARTICLES

FREE .NET MAGAZINES

Free DNC .NET Magazine

Tags

JQUERY COOKBOOK

jQuery CookBook