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

半腔热情 提交于 2019-12-17 06:43:08

问题


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


回答1:


Using the SetDefaultPrinter Windows API.

Here's how to pInvoke that.




回答2:


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);

    }
}



回答3:


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;



回答4:


Here is how it can be done with C# .NET without using the Win32API within the scope of a .NET application. The Win32API approach retains the default printer After the application is closed.

using System.Drawing.Printing;

namespace MyNamespace
{
  public class MyPrintManager
  {
    public static PrinterSettings MyPrinterSettings = new PrinterSettings();

    public static string Default_PrinterName
    {
      get
      {
        return MyPrinterSettings.PrinterName;
      }
      set
      {
        MyPrinterSettings.DefaultPageSettings.PrinterSettings.PrinterName = value;
        MyPrinterSettings.PrinterName = value;
      }
    }
  }
}



回答5:


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



来源:https://stackoverflow.com/questions/971604/how-do-i-set-the-windows-default-printer-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!