开发者

how to set to default printer

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.


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.


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


GetDefaultPrinter() 

{ PrinterSettings settings = new PrinterSettings(); 

foreach (string printer in PrinterSettings.InstalledPrinters) 

{ settings.PrinterName = printer; 

if (settings.IsDefaultPrinter) 

return printer; 

} 

return string.Empty; 

}


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


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜