如何将PrintDocument.PrinterSettings.PrinterName
设置为默认打印机?
我不是在谈论如何在操作系统中设置默认打印机,而是在谈论如何设置PrintDocument对象以便它可以打印到默认打印机。
如何将PrintDocument.PrinterSettings.PrinterName
设置为默认打印机?
我不是在谈论如何在操作系统中设置默认打印机,而是在谈论如何设置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之类的替代技术,但我认为这对您来说是最简单和最清晰的解决方案。
它会自动初始化为默认打印机。不需要进行任何操作。
GetDefaultPrinter()
{ PrinterSettings settings = new PrinterSettings();
foreach (string printer in PrinterSettings.InstalledPrinters)
{ settings.PrinterName = printer;
if (settings.IsDefaultPrinter)
return printer;
}
return string.Empty;
}
如果我错了,请纠正我,但您想要获取默认打印机的名称,然后将PrintDocument.PrinterSettings.PrinterName
设置为此名称。
当您使用PrintDocument.PrinterSettings.PrinterName
时,默认情况下会使用默认打印机。
如果您在对象上没有设置任何内容,那么默认情况下您将会落在默认打印机上。这是您正在寻找的官方来源:MSDN 链接到 PrintDocument 类
请注意上面示例中写的句子:"以下代码示例将文件 C:\My Documents\MyFile.txt 打印在默认打印机上。"
希望对您有所帮助。
我假设您已在操作系统层面上设置了默认打印机。当您从代码中发起打印时,默认情况下会将其发送到默认打印机。您不必显式地进行设置。
对于每个打印请求,都会发生这种情况。也就是说,如果您已将打印设置为另一个打印机,现在想要返回默认打印机,只需删除显式设置即可,它将再次回到默认打印机。
希望对您有所帮助。