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 )


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

Published by


A developer,teacher and a blogger obsessed with Python,Dart and open source world from India

Leave a Reply

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

You are commenting using your 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.