Last updated: 19th June 2024
Microsoft’s .Net framework provide the necessary properties and function to get a list of network printers, installed at the server side as well as local printers. These methods are widely applicable and used particularly on Intranet or using Window’s Applications. You can also use these methods for your intranet web (browser based) application, to get the list of printers (with other information) installed on a remote machine.Here in this article, I am going to show you two different methods to get the list of printers installed on a server. These printers are either shared on the network or are installed on the server itself.
01) The First Method
The PrinterSettings class of namespace System.Drawing.Printing will provide us with the list of installed printers on the Network. In the below example, I have added two controls a ComboBox and a ListBox on my web page. I’ll store the extracted list of printers in these two controls.
First, drag and drop a "ComboBox" and "ListBox" control on the form.
Copy and paste the below code and run application. It will show you a list of printers installed in your computer.
using System; using System.Windows.Forms; private void Form1_Load(object sender, EventArgs e) { PrinterList(); } private void PrinterList() { // POPULATE THE COMBO BOX. foreach (string sPrinters in System.Drawing.Printing.PrinterSettings.InstalledPrinters) { cmbPrinterList.Items.Add(sPrinters); } // POPULATE THE LIST BOX. foreach (string sPrinters in System.Drawing.Printing.PrinterSettings.InstalledPrinters) { lstBoxPrinters.Items.Add(sPrinters); } }
Option Explicit On Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load PrinterList() End Sub Private Sub PrinterList() Dim sPrinters As String = "" ' POPULATE THE COMBO BOX. For Each sPrinters In System.Drawing.Printing.PrinterSettings.InstalledPrinters cmbPrinterList.Items.Add(sPrinters) Next ' POPULATE THE LIST BOX. For Each sPrinters In System.Drawing.Printing.PrinterSettings.InstalledPrinters lstBoxPrinters.Items.Add(sPrinters) Next End Sub
02) The Second Method
Now this method uses the Windows Management Instrumentation or the WMI interface. It’s a technology used to get information about various systems (hardware) running on a "Windows Operating System".
Ref: Learn more about Windows Management Instrumentation (WMI).
Note: Before you start writing your code, add the System.Management reference to you project. This can be done by Right clicking the project and select “Add Reference…” from the “Solution Explorer”. On the “Add Reference” window, select .Net tab and search for “System.Management” from the list of “Component Name” and click “OK”.
In this example, I am using two separate ComboBoxes to show the Network and Local printers seperately.
➡️ To test this method, please check if there are printers installed on the Network.
using System; using System.Windows.Forms; using System.Management; namespace Printers { public partial class Form1 : Form { public Form1() {InitializeComponent();} private void Form1_Load(object sender, EventArgs e) { PrinterList(); } private void PrinterList() { // USING WMI. (WINDOWS MANAGEMENT INSTRUMENTATION) System.Management.ManagementScope objMS = new System.Management.ManagementScope(ManagementPath.DefaultPath); objMS.Connect(); SelectQuery objQuery = new SelectQuery ("SELECT * FROM Win32_Printer"); ManagementObjectSearcher objMOS = new ManagementObjectSearcher(objMS, objQuery); System.Management.ManagementObjectCollection objMOC = objMOS.Get(); foreach (ManagementObject Printers in objMOC) { if (Convert.ToBoolean(Printers["Local"])) // LOCAL PRINTERS. { cmbLocalPrinters.Items.Add(Printers["Name"]); } if (Convert.ToBoolean(Printers["Network"])) // ALL NETWORK PRINTERS. { cmbNetworkPrinters.Items.Add(Printers["Name"]); } } } } }
Option Explicit On Imports System.Management Public Class Form1 Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load PrinterList() End Sub Private Sub PrinterList() 'POPULATE A COMBO BOX WITH PRINTER LIST. For Each sPrinters In System.Drawing.Printing.PrinterSettings.InstalledPrinters cmbPrinterList.Items.Add(sPrinters) Next 'POPULATE A LIST BOX WITH PRINTER LIST. For Each sPrinters In System.Drawing.Printing.PrinterSettings.InstalledPrinters lstBoxPrinters.Items.Add(sPrinters) Next ' USING WMI. (Windows Management Instrumentation) Dim objMS As System.Management.ManagementScope = _ New System.Management.ManagementScope(ManagementPath.DefaultPath) objMS.Connect() Dim objQuery As SelectQuery = New SelectQuery ("SELECT * FROM Win32_Printer") Dim objMOS As ManagementObjectSearcher = New ManagementObjectSearcher(objMS, objQuery) Dim objMOC As System.Management.ManagementObjectCollection = objMOS.Get() For Each Printers As ManagementObject In objMOC If CBool(Printers("Local")) Then ' LOCAL PRINTERS. cmbLocalPrinters.Items.Add(Printers("Name")) End If If CBool(Printers("Network")) Then ' ALL NETWORK PRINTERS. cmbNetworkPrinters.Items.Add(Printers("Name")) End If Next Printers End Sub End Class
Well, that’s it. We just learned how to find network and local printers installed around us, using various .Net framework classes, properties and functions. I hope the article and its codes would help you find a solution.