C# Get installed Printers list in windows application form

csharp get installed printers list Get list of printer name installed in the computer and populate in combobox dropdown, get the default printer name and set the printer name on top.
Get printer current settings paper size, orientation landscape or portrait, number of prints, print resolution.
Change default printer on combobox SelectedIndexChanged event.

Download

Example Source Snippets - GetPrinterList.zip

namespace : using System.Drawing.Printing

Installed Printer List in PC


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.Runtime.InteropServices;

namespace GetPrinterList
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            GetPrintersInstalled(true); // get insalled printer list and default printer datas
        }

        private void PrinterCbo_SelectedIndexChanged(object sender, EventArgs e)
        {
            ChangeDefaultPrinter(PrinterCbo.Text); //set as default printer
        }

        private void GetPrintersInstalled(bool isFirstTime)
        {

            try
            {
                PrintDocument printDoc = new PrintDocument();
                string defaultPrinterName = null;

                for (int i = 0; i < PrinterSettings.InstalledPrinters.Count; i++)
                {
                    if (isFirstTime)
                        PrinterCbo.Items.Add(PrinterSettings.InstalledPrinters[i]);

                    if (printDoc.PrinterSettings.IsDefaultPrinter)//get default printer at form loading event                    
                        defaultPrinterName = printDoc.PrinterSettings.PrinterName;
                }

                if (defaultPrinterName != null)
                {
                    PrinterCbo.Text = defaultPrinterName;
                    lblDefaultPrinter.Text = PrinterCbo.Text;
                    lblNoOfCopy.Text = printDoc.DefaultPageSettings.PrinterSettings.Copies.ToString();
                    lblPaperSize.Text = printDoc.DefaultPageSettings.PaperSize.PaperName.ToString();
                    lblResolution.Text = printDoc.DefaultPageSettings.PrinterResolution.ToString();
                    
                    if (printDoc.DefaultPageSettings.Landscape)
                        lblOrientation.Text = "LandScape";
                    else
                        lblOrientation.Text = "Portrait";
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        // Change default printer
        [DllImport("Winspool.drv")]
        private static extern bool SetDefaultPrinter(string printerName);
        private void ChangeDefaultPrinter(string printerName)
        {
            if (printerName != null)
            {
                SetDefaultPrinter(printerName);
                GetPrintersInstalled(false);
            }
        }
    }
}

       

namespace : using System.Runtime.InteropServices

Set printer as default programmatically


         // Change default printer
        [DllImport("Winspool.drv")]
        private static extern bool SetDefaultPrinter(string printerName);
        private void ChangeDefaultPrinter(string printerName)
        {
            if (printerName != null)
            {
                SetDefaultPrinter(printerName);
                GetPrintersInstalled(false);
            }
        }
                    

smartsnipps.ecomparefiles.com © 2019, All Rights Reserved | Disclaimer: smartsnipps.eCompareFiles.com is free to use any code snippets without guarantee