For printing tasks, it’s common to need to fetch the list of currently installed printers on a given PC. The Windows’ PrintDialog does display available printers—but it’s generally intended to be used in the general workflow of printing a document. The PrintDialog is also no help whatsoever for browser-hosted intranet apps where a user needs to select, and then print, to a given network printer. What I wanted was an easy way to show users available printers; and I wanted this technique to work in either Windows or browser clients.
For example, the other day I worked on a task where the requirement was to be able to batch print documents (Word and Excel mostly, but also images). In this case, the PrintDialog wasn’t much help. I wanted to provide an easy way for users to select the target printer without all of the ceremony of the PrintDialog.
To perform this task, I wrote the code shown in Figure 1. It provides two classes:
- PrinterCommon. This class provides a GetInstalledPrintersList() method that returns an instance of a class that provides a string array of printer names and an integer value indicating which of the printers in the list is the default printer. (I named this class PrinterCommon because I intend later to add other common print tasks to it.)
- InstalledPrinters. This class is the result type returned from from GetInstalledPrinterList(). While I typically don’t like to put more than one class in a source member, given the tight coupling between PrinterCommon and InstalledPrinters, that seemed like an OK shortcut for this simple task.
1using System;
2using System.Drawing.Printing;
3
4namespace rp
5{
6 /// <summary>
7 /// Common print and printer tasks.
8 /// </summary>
9 internal class PrinterCommon
10 {
11 /// <summary>
12 /// Get the list of installed printers.
13 /// </summary>
14 /// <returns>An instance of InstalledPrinters.</returns>
15 public static InstalledPrinters GetInstalledPrinterList()
16 {
17 // Get default printer name.
18 string currentPrinter = new PrinterSettings().PrinterName;
19
20 // Declare a string array to store the list of installed printers.
21 var installedPrinters =
22 new string[ PrinterSettings.InstalledPrinters.Count ];
23
24 // Copy the list of installed printers into the installedPrinters
25 // array (starting at element zero).
26 PrinterSettings.InstalledPrinters.CopyTo( installedPrinters, 0 );
27
28 // Get index of default printer.
29 int i = Array.IndexOf( installedPrinters, currentPrinter );
30
31 // Return with a new instance of InstalledPrinters.
32 return new InstalledPrinters() { PrinterNames = installedPrinters,
33 SelectedPrinterIndex = i };
34 }
35 }
36
37 /// <summary>
38 /// A simple class used to list the installed printers
39 /// and the index in that list of the default printer.
40 /// </summary>
41 class InstalledPrinters
42 {
43 public string[] PrinterNames { get; set; }
44 public int SelectedPrinterIndex { get; set; }
45 }
46}
The code to use the GetInstalledPrinterList() is very easy; it is shown below in Figure 2. Note that the Item’s AddRange() method is used to assign the array of printer names to the ComboBox (which also works with ListBox). While the code in Figure 2 is specific to Windows, a simple variation of it works just fine with browser-based applications.
1rp.InstalledPrinters pl = rp.PrinterCommon.GetInstalledPrinterList();
2
3comboBox1.Items.AddRange( pl.PrinterNames );
4comboBox1.SelectedIndex = pl.SelectedPrinterIndex;



