C # Enumerate all printers installed by the system

Source: Internet
Author: User
Tags foreach variable
Print recently in the forum many netizens asked "How to install all the printers in Windows listed", in the following program we will be installed in the system of the printer listed in the list box, while the default printer set the default value.

In the following program we used two main classes, all of the printer list out of the PrinterSettings class, get the system default printer used PrintDocument class, let's do a hands-on.

To create a new Windows Form project, and then join a lable and a combox, it's OK, the key is below, how we get the default printer, we have to use the following statement.

PrintDocument Prtdoc = new PrintDocument ();
String strdefaultprinter = Prtdoc. printersettings.printername;//get the default printer name

With the default printer, we'll list all the printers.

PrinterSettings class has a InstalledPrinters property, do not know what it is to do, check MSDN as explained below:
Printersettings.installedprinters gets the names of all the printers installed on the computer.

The following definitions are defined in C #:

[C #]
[Serializable]
[ComVisible (False)]
public static Printersettings.stringcollection InstalledPrinters
{get;}

Property value

Printersettings.stringcollection, which represents the names of all printers installed on the computer.

Abnormal

Exception type condition
Win32Exception failed to enumerate available printers

Note

You can use a collection of installed printer names to provide the user with a choice of printer to print to.

The following example populates the Comboinstalledprinters combo box with an installed printer, and also uses the PrinterName property to set the printer for printing when the change is selected. The PopulateInstalledPrintersCombo routine is invoked when the form is initialized. The example assumes that there is a PrintDocument variable named Printdoc, and that a specific combo box exists.

[C #]
The following brackets are added to their own translations
private void PopulateInstalledPrintersCombo ()
{
ADD list of installed printers found to the combo box. (Add all the machines in the system to the list box)
The pkinstalledprinters string is used to provide the display string. (The string displayed in the list box is provided by Pkinstalledprinters)
foreach (String pkinstalledprinters in
Printersettings.installedprinters)
{
COMBOINSTALLEDPRINTERS.ITEMS.ADD (pkinstalledprinters);
}
}

private void Comboinstalledprinters_selectionchanged (object sender, System.EventArgs e)
{

Set the printer to a printer in the combo box when the selection changes. (Set the selected printer when the list box changes)

if (Comboinstalledprinters.selectedindex!=-1)
{
The combo box ' s Text property returns the selected item ' s text and which is the printer name. (Displays the selected printer name in the list box)
Printdoc.printersettings.printername= Comboinstalledprinters.text;
}

}

Read the MSDN instructions, understand a lot of it, here is the complete code I write practice.

Program Description: All printers in the system are listed in the list box
Program variables: PrintDocument Prtdoc, String Strdefaultprinter
Author: silkworm Chrysalis (Sillnet@163.net)
Date: 2003-03-20
Using System;
Using System.Drawing;
Using System.Drawing.Printing;
Using System.Collections;
Using System.ComponentModel;
Using System.Windows.Forms;
Using System.Data;

Namespace Printerlist
{
///
Summary description of the Form1.
///
public class Form1:System.Windows.Forms.Form
{
Private System.Windows.Forms.Label Label1;
Private System.Windows.Forms.ComboBox printerlist;
///
The required designer variable.
///
Private System.ComponentModel.Container components = null;

Public Form1 ()
{
//
Required for Windows Forms Designer support
//
InitializeComponent ();
PrintDocument Prtdoc = new PrintDocument ();
String strdefaultprinter = Prtdoc. printersettings.printername;//get the default printer name
foreach (String strprinter in Printersettings.installedprinters)
Lists all the printers in the list box,
{
PRINTERLIST.ITEMS.ADD (Strprinter);
if (Strprinter = = strdefaultprinter)//The default printer is set to the default value
{
Printerlist.selectedindex = PrinterList.Items.IndexOf (Strprinter);
}
}
//
TODO: Add any constructor code after the InitializeComponent call
//
}

///
Clean up all resources that are in use.
///
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (Components!= null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing);
}

#region Windows Form Designer generated code
///
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
///
private void InitializeComponent ()
{
This.label1 = new System.Windows.Forms.Label ();
This.printerlist = new System.Windows.Forms.ComboBox ();
This. SuspendLayout ();
//
Label1
//
This.label1.Location = new System.Drawing.Point (8, 24);
This.label1.Name = "Label1";
This.label1.Size = new System.Drawing.Size (72, 16);
This.label1.TabIndex = 0;
This.label1.Text = "Select printer:";
//
Printerlist
//
This.printerList.Location = new System.Drawing.Point (88, 22);
This.printerList.Name = "Printerlist";
This.printerList.Size = new System.Drawing.Size (192, 21);
This.printerList.TabIndex = 1;
This.printerList.Text = "Current system not installed printer";
//
Form1
//
This. AutoScaleBaseSize = new System.Drawing.Size (5, 13);
This. ClientSize = new System.Drawing.Size (288, 61);
This. Controls.AddRange (new system.windows.forms.control[] {
This.printerlist,
This.label1});
This. Name = "Form1";
This. Text = "Printer List";
This. ResumeLayout (FALSE);

}
#endregion

///
The main entry point for the application.
///
[STAThread]
static void Main ()
{
Application.Run (New Form1 ());
}

}
}

The above code is tested under Windows XP + Vc.net, and is compiled and tested on WINDOWS98


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.