如何在 C# 中设置 Windows 默认打印机?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/971604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I set the windows default printer in C#?
提问by jms
How do I set the windows default printer in C#.NET?
如何在 C#.NET 中设置 Windows 默认打印机?
采纳答案by rein
Using the SetDefaultPrinter Windows API.
使用 SetDefaultPrinter Windows API。
回答by Cheeso
You can use WMI as well.
http://cheeso.members.winisp.net/srcview.aspx?file=printer.cs
您也可以使用 WMI。
http://cheeso.members.winisp.net/srcview.aspx?file=printer.cs
回答by Cheeso
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);
}
}
回答by gridtrak
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.
下面是如何在 C# .NET 中完成它而不使用 .NET 应用程序范围内的 Win32API。Win32API 方法在应用程序关闭后保留默认打印机。
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;
}
}
}
}
回答by Muhammad Abbas
Step 1: Paste the following code anywhere in your .cs file
第 1 步:将以下代码粘贴到 .cs 文件中的任何位置
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
第 2 步:添加必要的命名空间,即
using System.Runtime.InteropServices;
Step 3: Use the following function to set desired printer as default printer.
步骤 3:使用以下功能将所需的打印机设置为默认打印机。
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.
第 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.
Namespaces required for the above code are:
上述代码所需的命名空间是:
using System.Drawing.Printing;
using System.Runtime.InteropServices;