簡體   English   中英

如何在 C# 中設置 Windows 默認打印機?

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

如何在 C#.NET 中設置 Windows 默認打印機?

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

    }
}

使用 SetDefaultPrinter Windows API。

這是 pInvoke 的方法。

第 1 步:將以下代碼粘貼到 .cs 文件中的任何位置

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

第 2 步:添加必要的命名空間,即

using System.Runtime.InteropServices;

步驟 3:使用以下功能將所需的打印機設置為默認打印機。

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

第 4 步:要獲取連接到 PC 的所有打印機的列表,您可以使用此代碼。

  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.

上述代碼所需的命名空間是:

using System.Drawing.Printing;
using System.Runtime.InteropServices;

這是我現在使用的方法。 我包括了 SetDefaultPrinter 方法來提高可靠性。 這是我從其他答案中得出的,並對其進行了修訂以反映對我以前的答案版本的反饋。

關於注釋:此方法僅適用於正在運行的 C# 應用程序會話的范圍內。 此方法不會更改由操作系統管理或存儲在注冊表中的打印機設置。

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

典型用法:

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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM