Set Printer as default in C#


As we learned how to list installed printers , also it is possible set printer as default at run time with some tricky C# code. This can be achieved by using winspool.drv functionality.

Firstly incorporate the Winspool functionality to C# by using DllImport.

  1. Add using System.Runtime.InteropServices
  2. Add a public class Myprinter and implement SetDefaultPrinter method (bottom of the namespace )

c#setdefaultprinter

public static class myPrinters
 {
 [DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
 public static extern bool SetDefaultPrinter(string Name);

}

Now you can simply set a printer as default using the following code.

myPrinters.SetDefaultPrinter(<yourprinter name>);

 

That is all for today

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.