How to set a default printer

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

I'm not talking about setting up the default printer in the operating system. Rather, I'm talking about setting up the PrintDocument object so that it prints to the default printer.

+5
source share
6 answers

If I understand correctly, you would like to be able to reset PrinterName to the default printer (1) without re-creating your PrintDocument and (2) after you possibly already installed it on something else or (3) when the printer the default could have been changed since the first PrintDocument was created (so you cannot rely on simple caching of the default values โ€‹โ€‹set by the target instance after the initial construction).

In this case, searching โ€œC # gets the default printer nameโ€ opens the following excellent entry in stackoverflow: What is the best way to get the default printer in .NET

Based on the sample provided in the upper voting answer, and considering that you already have an existing PrintDocument with some settings that you do not want to recreate; you can create a new instance of the PrinterSettings class for the sole purpose of copying 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 PrinterSettings classes. existingPrintDocumentInstance.PrinterSettings.PrinterName = newSettings.PrinterName; 

You can view the related post for alternative methods like WMI, but I think this is the simplest and cleanest solution for you.

+9
source

It is automatically initialized by default printer. Do nothing.

+4
source
 GetDefaultPrinter() { PrinterSettings settings = new PrinterSettings(); foreach (string printer in PrinterSettings.InstalledPrinters) { settings.PrinterName = printer; if (settings.IsDefaultPrinter) return printer; } return string.Empty; } 
+2
source

I assume that you have installed the default printer at the OS level. When you start printing from your code, it defualt goes to the default printer. You do not need to explicitly specify it.

This will happen for every print request. I mean, if you set printing to another printer and now want to switch to the default printer, just delete the explicit setting and it will go back to the default printer again.

NTN

0
source

Correct me if I am wrong, but you want to get the default printer name, and then set PrintDocument.PrinterSettings.PrinterName for it.

When using PrintDocument.PrinterSettings.PrinterName , the default printer is used.

0
source

By default, you will land on the default printer if you do not install anything on your subject. Here is the official source you were looking for: MSDN Link to PrintDocument Class

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

NTN

0
source

Source: https://habr.com/ru/post/955781/


All Articles