如何设置为默认打印机

如何设置为默认打印机

问题描述:

如何将PrintDocument.PrinterSettings.PrinterName设置为默认打印机?

How do you set PrintDocument.PrinterSettings.PrinterName to be the default printer?

我不是在谈论在操作系统中设置默认打印机.相反,我在谈论设置PrintDocument对象,以便将其打印到默认打印机.

I am not talking about setting the default printer in the operating system. Rather, I am talking about setting the PrintDocument object so that it prints to the default printer.

如果我正确理解,您希望能够将PrinterName重置为默认打印机(1),而无需重新创建PrintDocument (2),并且您可能已经将其设置为其他设置 ,或者 (3). .

If I'm understanding correctly, you would like to be able to reset the PrinterName to the default printer (1) without recreating your PrintDocument and, (2) after you may have already set it to something else or, (3) when the default printer may have changed since the time when the PrintDocument was first created (so you can't rely on simply caching the defaults provided by the target instance after initial construction).

在这种情况下,搜索" C#获取默认打印机名称"会在*上显示以下出色的文章:

In this case a search for "C# get default printer name" turns up the following excellent post on *: What's the best way to get the default printer in .NET

基于在投票最多的答案中提供的示例,并考虑到您已经具有一个已有的PrintDocument,其中某些设置您不想重新创建;您可以创建PrinterSettings类的新实例,仅出于复制默认打印机名称的目的.

Building on the sample provided in top voted answer and considering that you will already have a pre-existing PrintDocument with some settings you don't want to recreate; you could create a new instance of the PrinterSettings class, for the sole purposes of copying out the default printer name.

// Create a new instance of the PrinterSettings class, which 
// we will only use to fetch the default printer name
System.Drawing.Printing.PrinterSettings newSettings = new System.Drawing.Printing.PrinterSettings();

// Copy the default printer name from our newSettings instance into our 
// pre-existing PrintDocument instance without recreating the 
// PrintDocument or the PrintDocument's PrinterSettings classes.
existingPrintDocumentInstance.PrinterSettings.PrinterName = newSettings.PrinterName;

您可以查看链接的文章,以了解WMI等替代技术,但是我认为这是最简单,最干净的解决方案.

You can review the linked post for alternative techniques such as WMI, but I think this is the simplest and cleanest solution for you.