Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 266
Craig Johnston Avatar asked Mar 02 '11 06:03

Craig Johnston


People also ask

What is default printer?

You may be asking yourself, what is a default printer? A default printer is a printer all print jobs are sent to unless otherwise specified. Having a default printer prevents a program from asking what printer they want to use each time they print.

How do I set HP printer to default?

Click the Windows icon ( ), click Devices and Printers. The Devices and Printers window opens. Right-click your HP product, and then click Set as default printer.

How do I choose a default printer in Windows?

The setting is available in Windows 10 version 1511 (build 10586) or later. Go to Settings > Devices > Printers & scanners, and switch off the “Let Windows my default printer” setting. Alternatively, you can also disable the setting through Group Policy if you have a larger network domain environment.


2 Answers

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.

like image 58
BenSwayne Avatar answered Oct 30 '22 17:10

BenSwayne


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

like image 45
Hans Passant Avatar answered Oct 30 '22 18:10

Hans Passant