Setting and retrieving the default printer from code

Hi,

There came a requirement in one of my colleague’s project whereby it was needed to set the printer to be used for the lotus notes (The project is about integrating with Lotus Notes.).  

To solve the issue instead of looking into the Lotus notes API I thought it should be much easy to set the default printer for the machine. Once the Default printer is set Lotus notes will use this printer to print any document.  Below is the code needed to set the default printer of any machine.

[DllImport("winspool.drv", SetLastError = true)]
private static extern void SetDefaultPrinter(string name);

To use the code, you need to import the System.Runtime.InteropServices namespace as below.

using System.Runtime.InteropServices;
You might also require to find the default printer in the machine. For that the code is given below.

System.Drawing.Printing.PrinterSettings oPS = new System.Drawing.Printing.PrinterSettings();
functionReturnValue = oPS.PrinterName;
Vikram
P.S. Thanks to Anindita for her Help with Code

1 Comment

Comments have been disabled for this content.