Subscribe

RSS Feed (xml)

Retrieve Information About the Installed Printers

The PrinterSettings class encapsulates the settings for a printer and information about the printer. For example, you can use the PrinterSettings class to determine supported paper sizes, paper sources, and resolutions and check for the ability to print color or double-sided (or duplexed) pages. In addition, you can retrieve default page settings for margins, page orientation, and so on.

The PrinterSettings class provides a static InstalledPrinters collection, which includes the name of every printer that's installed on the computer. If you want to find out more information about the settings for a specific printer, you simply need to create a PrinterSettings instance and set the PrinterName property accordingly.

The following code shows a Console application that finds all the printers that are installed on a computer and displays information about the paper sizes and the resolutions supported by each one.

using System;
using System.Drawing.Printing;

public class ListPrinters {

    private static void Main(string[] args) {
    
        foreach (string printerName in PrinterSettings.InstalledPrinters) {
        
            // Display the printer name.
            Console.WriteLine("Printer: {0}", printerName);

            // Retrieve the printer settings.
            PrinterSettings printer = new PrinterSettings();
            printer.PrinterName = printerName;

            // Check that this is a valid printer.
            // (This step might be required if you read the printer name
            // from a user-supplied value or a registry or configuration file
            // setting.)
            if (printer.IsValid) {
            
                // Display the list of valid resolutions.
                Console.WriteLine("Supported Resolutions:");

                foreach (PrinterResolution resolution in
                  printer.PrinterResolutions) {
                    Console.WriteLine("  {0}", resolution);
                } 
                Console.WriteLine();

                // Display the list of valid paper sizes.
                Console.WriteLine("Supported Paper Sizes:");
                
                foreach (PaperSize size in printer.PaperSizes) {
                
                    if (Enum.IsDefined(size.Kind.GetType(), size.Kind)) {
                        Console.WriteLine("  {0}", size);
                    }
                }
                Console.WriteLine();
            }
        }
        Console.ReadLine();
    }
}

Here's the type of output this utility displays:

Printer: HP LaserJet 5L
Supported Resolutions:
  [PrinterResolution High]
  [PrinterResolution Medium]
  [PrinterResolution Low]
  [PrinterResolution Draft]
  [PrinterResolution X=600 Y=600]
  [PrinterResolution X=300 Y=300]

Supported Paper Sizes:
  [PaperSize Letter Kind=Letter Height=1100 Width=850]
  [PaperSize Legal Kind=Legal Height=1400 Width=850]
  [PaperSize Executive Kind=Executive Height=1050 Width=725]
  [PaperSize A4 Kind=A4 Height=1169 Width=827]
  [PaperSize Envelope #10 Kind=Number10Envelope Height=950 Width=412]
  [PaperSize Envelope DL Kind=DLEnvelope Height=866 Width=433]
  [PaperSize Envelope C5 Kind=C5Envelope Height=902 Width=638]
  [PaperSize Envelope B5 Kind=B5Envelope Height=984 Width=693]
  [PaperSize Envelope Monarch Kind=MonarchEnvelope Height=750 Width=387]

Printer: Generic PostScript Printer
. . .

Technorati :

1 comment:

Archives

LocalsAdda.com-Variety In Web World

Fun Mail - Fun in the Mail