Enlist Printer(s) on the System


Description:

About Classes used –
#1: An object of PrintDocument class, allow for setting the properties that describe how to print, and call the Print method to start the printing process.
#2:  PrinterSettings class, Specifies information about how a document is printed, including
     the printer that prints it, when printing from a Windows Forms application.
Namespace Required – System.Drawing.Printing
Controls used:
1.    ListView Control (lvPrinters)
2.    Button Control (btnGetPrinter & btnExit)
Here I implemented the Code for enlisting the printer(s) installed on the system.
           
The Code: (do necessary changes)
1. Variables Declaration.
                        Boolean value = true;
                        String Default = “”;
Listing 1
2. Search for Default Printer.
                   // Determine Default Printer Exist or Not
                   if ((newPrintDocument()).PrinterSettings.IsDefaultPrinter == value)
            {
            // if Exist Fetch the Printer’s Name
                  ListViewItem item = new ListViewItem(Default =
                            (new PrintDocument()).PrinterSettings.PrinterName);
                  item.SubItems.Add(value.ToString());              
                  // Add the Default Printer’s Name to ListView
                  lvPrinters.Items.Add(item);                
                 
            value = false; // Reset Boolean Value to False
            }
Listing 2
3. Search for Other Printer(s)
                   //Fetch All Installed Printer’s Name
                   foreach (objectvar in PrinterSettings.InstalledPrinters)
            {
                  // Compare Installed Printer(s) Name with Default Printer
                  if (Default != var.ToString())
                  {
                        ListViewItem item = newListViewItem(var.ToString());
                        item.SubItems.Add(value.ToString());
                        // Add Remaining Printer’s Name to ListView
                        lvPrinters.Items.Add(item);
                  }
            }
Listing 3
   
4. Now execute the Application and see the result (Figure 1).
         
Intended Result:
Figure 1
Summary:
          In this piece of writing, using C# environment, we have seen how to retrieve printer(s) name installed on the system.
Regards,
Mayur Dighe
http://ImmortalDotNet.blogspot.com
http://MayurDighe.wordpress.com