Here in this article, I am going to explain with examples, two different methods to get the list of printers installed on a server. These printers either are shared on the network or are installed on the server itself.
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.
Bind and Show a ComboBox in a DataGridView Cell – Windows Application
Now, drag and drop a ComboBox and ListBox control on the form.
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
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 2 separate ComboBox controls to show the Network and Local printers seperately.
Note: 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.
Thanks for reading. ☺