how to set to default printer

Craig Johnston picture Craig Johnston · Mar 2, 2011 · Viewed 20.2k times · Source

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.

Answer

BenSwayne picture BenSwayne · Dec 1, 2011

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.