Get and Display List of Available Printers using C#
Submitted by donbermoy on Saturday, June 14, 2014 - 13:08.
Today in C#, i will going to teach you how to create a program that gets and displays the list of available printers.
Now, let's start this tutorial!
1. Let's start with creating a Windows Form Application in C# for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application and name your project as Get Printers.
2. Next, add only one Button named Button1 and labeled it as "Get Printers" and one ListBox named ListBox1 that will serve as the display in the lists of printers. You must design your interface like this:
3. Import System.Drawing.Printing namespace because we will use the PrinterSettings class here.
4. Now put this code for your code module.
When you print from a Windows Forms application, you create a new instance of the PrintDocument class, set properties, such as DefaultPageSettings and PrinterSettings, that describe how to print, and call the Print method to actually print the document. We will have to initialize a variable index1 as object and will create a For Loop statement that will start to 0 up to all the lists of installed printers. PrintDocument Class defines a reusable object that sends output to a printer, when printing from a Windows Forms application. Then the list of the printers will be added to the listview to display it all using the ListBox control in C#.
Full source code:
Press F5 to run the program.
Best Regards,
Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]
Add and Follow me on Facebook: https://www.facebook.com/donzzsky
Visit and like my page on Facebook at: https://www.facebook.com/BermzISware
- using System.Diagnostics;
- using System;
- using System.Windows.Forms;
- using System.Collections;
- using System.Drawing;
- using System.Data;
- using System.Collections.Generic;
- using System.Drawing.Printing;
- public void Button1_Click(System.Object sender, System.EventArgs e)
- {
- object index1 = default(object);
- for (index1 = 0; (int) index1 <= PrinterSettings.InstalledPrinters.Count; index1 = (int) index1 + 1)
- {
- ListBox1.Items.Add(PrinterSettings.InstalledPrinters[System.Convert.ToInt32(index1)]);
- }
- }
- using System.Diagnostics;
- using System;
- using System.Windows.Forms;
- using System.Collections;
- using System.Drawing;
- using System.Data;
- using System.Collections.Generic;
- using System.Drawing.Printing;
- namespace WindowsApplication1
- {
- public partial class Form1
- {
- public Form1()
- {
- InitializeComponent();
- }
- public void Button1_Click(System.Object sender, System.EventArgs e)
- {
- object index1 = default(object);
- for (index1 = 0; (int) index1 <= PrinterSettings.InstalledPrinters.Count; index1 = (int) index1 + 1)
- {
- ListBox1.Items.Add(PrinterSettings.InstalledPrinters[System.Convert.ToInt32(index1)]);
- }
- }
- }
- }
Add new comment
- 301 views