How do I set the windows default printer in C#?

前端 未结 5 1765
抹茶落季
抹茶落季 2020-11-29 06:24

How do I set the windows default printer in C#.NET?

相关标签:
5条回答
  • 2020-11-29 06:59
    using System;
    using System.Drawing.Printing;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
    
            private void listAllPrinters()
            {
                foreach (var item in PrinterSettings.InstalledPrinters)
                {    
                    this.listBox1.Items.Add(item.ToString());
                }
            }
    
            private void listBox1_SelectedValueChanged(object sender, EventArgs e)
            {
                string pname = this.listBox1.SelectedItem.ToString();
                myPrinters.SetDefaultPrinter(pname);
            }
    
    
            public Form1()
            {
                InitializeComponent();
                listAllPrinters();
            }
        }
    
        public static class myPrinters
        {
            [DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
            public static extern bool SetDefaultPrinter(string Name);
    
        }
    }
    
    0 讨论(0)
  • 2020-11-29 07:01

    This is the method I use now. I include the SetDefaultPrinter method to improve reliability. I derived this from other answers, and it is revised to reflect feedback for my previous answer version.

    Regarding comments: This method is appropriate only inside the scope of a running C# application session. This method does not change printer settings managed by the operating system or stored in the registry.

    using System.Drawing.Printing;
    
    [DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern bool SetDefaultPrinter(string Name);
    
    public static PrinterSettings Printer_Settings = new System.Drawing.Printing.PrinterSettings();
    
    /// <summary>
    /// Get or Sets the session's Default Printer
    /// </summary>
    public static string Session_DefaultPrinter
    {
      get { return Printer_Settings.PrinterName; }
      set
      {
        SetDefaultPrinter(value);
        Printer_Settings.DefaultPageSettings.PrinterSettings.PrinterName = value;
        Printer_Settings.PrinterName = value;
      }
    }
    

    Typical Usage:

    string stashPrinterName = Session_DefaultPrinter;
    // Switch to my Special Printer
    Session_DefaultPrinter = mySpecialPrinter;
    // print to my Special Printer
    // ...
    // Restore the original session's printer
    Session_DefaultPrinter = stashPrinterName;
    
    0 讨论(0)
  • 2020-11-29 07:03

    Using the SetDefaultPrinter Windows API.

    Here's how to pInvoke that.

    0 讨论(0)
  • 2020-11-29 07:13

    Step 1: Paste the following code anywhere in your .cs file

      public static class PrinterClass
        {
            [DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
            public static extern bool SetDefaultPrinter(string Printer);
        }
    

    Step 2: Add the neccesary namespace i.e

    using System.Runtime.InteropServices;
    

    Step 3: Use the following function to set desired printer as default printer.

     PrinterClass.SetDefaultPrinter("Paste your desired Printer Name here");
    

    Step 4: To get the list of all printers attached to your PC, you can use this code.

      private void FillListBox()
        {
            foreach (var p in PrinterSettings.InstalledPrinters)
            {
                cmbdefaultPrinter.Properties.Items.Add(p);
            }
        } 
    //Here cmbdefaultPrinter is a combobox, you can fill the values into a list.
    

    Namespaces required for the above code are:

    using System.Drawing.Printing;
    using System.Runtime.InteropServices;
    
    0 讨论(0)
  • 2020-11-29 07:14

    You can use WMI as well.
    http://cheeso.members.winisp.net/srcview.aspx?file=printer.cs

    0 讨论(0)
提交回复
热议问题