如何设置为默认打印机

发布于 2024-10-20 00:58:38 字数 144 浏览 1 评论 0原文

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

我不是在谈论在操作系统中设置默认打印机。相反,我讨论的是设置 PrintDocument 对象,以便它打印到默认打印机。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

两仪 2024-10-27 00:58:38

如果我理解正确,您希望能够将 PrinterName 重置为默认打印机(1),而无需重新创建 PrintDocument (2)在您可能已经将其设置为其他内容之后 或者, (3) 当默认打印机自首次创建 PrintDocument 以来可能已更改 < em>(因此您不能仅仅依赖于在初始构建后简单地缓存目标实例提供的默认值)。

在这种情况下,搜索“C# get default Printer name”会在 stackoverflow 上找到以下优秀帖子:在 .NET 中获取默认打印机的最佳方法是什么

基于投票最高的答案中提供的示例并考虑到您已经有一个预先存在的 PrintDocument,其中包含一些您不想重新创建的设置;您可以创建 PrinterSettings 类的新实例,其唯一目的是复制默认打印机名称。

// 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 等替代技术,但我认为这对您来说是最简单、最干净的解决方案。

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

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

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;

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

他不在意 2024-10-27 00:58:38

它会自动初始化为默认打印机。什么都不做。

It is automatically initialized to the default printer. Do nothing.

甜嗑 2024-10-27 00:58:38
GetDefaultPrinter() 

{ PrinterSettings settings = new PrinterSettings(); 

foreach (string printer in PrinterSettings.InstalledPrinters) 

{ settings.PrinterName = printer; 

if (settings.IsDefaultPrinter) 

return printer; 

} 

return string.Empty; 

}
GetDefaultPrinter() 

{ PrinterSettings settings = new PrinterSettings(); 

foreach (string printer in PrinterSettings.InstalledPrinters) 

{ settings.PrinterName = printer; 

if (settings.IsDefaultPrinter) 

return printer; 

} 

return string.Empty; 

}
蓝海似她心 2024-10-27 00:58:38

我假设您已在操作系统级别设置默认打印机。当您从代码启动打印时,它默认转到默认打印机。您不必明确设置它。

每个打印请求都会发生这种情况。我的意思是,如果您已将打印设置为另一台打印机,现在想要转到默认打印机,只需删除显式设置,它将再次转到默认打印机。

华泰

I assume you have set the default printer at the OS level. When you initiate a print from your code, it by defualt goes to Default Printer. You don't have to set it explicitly.

This happend for each print request. I mean if you have set the print to another printer and now you want to go to the default printer, just remove the explicit setting and it will again go to the default printer.

HTH

遗忘曾经 2024-10-27 00:58:38

如果我错了,请纠正我,但您希望获取默认打印机的名称,然后将 PrintDocument.PrinterSettings.PrinterName 设置为此。

当您使用 PrintDocument.PrinterSettings.PrinterName 时,默认情况下会使用默认打印机。

Correct me if I am wrong but you are looking to get the name of the default printer and then setting PrintDocument.PrinterSettings.PrinterName to this.

When you use PrintDocument.PrinterSettings.PrinterName this uses the default printer by default.

余生共白头 2024-10-27 00:58:38

默认情况下,如果您未在对象上设置任何内容,您将登陆默认打印机。以下是您正在寻找的官方来源: MSDN 链接到 PrintDocument 类

标记示例上方的句子:“以下代码示例在默认打印机上打印名为 C:\My Documents\MyFile.txt 的文件。”

华泰

By default you would be landing on default printer if you do not set anything on your object. Here is the official source you were looking for: MSDN Link to PrintDocument Class

Mark the sentence written just above the example: "The following code example prints the file named C:\My Documents\MyFile.txt on the default printer."

HTH

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文