How do I set the windows default printer in C#.NET?
Asked
Active
Viewed 4.8k times
4 Answers
36
using System;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private void listAllPrinters()
{
foreach (var item in PrinterSettings.InstalledPrinters)
{
this.listBox1.Items.Add(item.ToString());
}
}
private void listBox1_SelectedValueChanged(object sender, EventArgs e)
{
string pname = this.listBox1.SelectedItem.ToString();
myPrinters.SetDefaultPrinter(pname);
}
public Form1()
{
InitializeComponent();
listAllPrinters();
}
}
public static class myPrinters
{
[DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool SetDefaultPrinter(string Name);
}
}

emregon
- 388
- 1
- 7
- 18
6
Step 1: Paste the following code anywhere in your .cs file
public static class PrinterClass
{
[DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool SetDefaultPrinter(string Printer);
}
Step 2: Add the neccesary namespace i.e
using System.Runtime.InteropServices;
Step 3: Use the following function to set desired printer as default printer.
PrinterClass.SetDefaultPrinter("Paste your desired Printer Name here");
Step 4: To get the list of all printers attached to your PC, you can use this code.
private void FillListBox()
{
foreach (var p in PrinterSettings.InstalledPrinters)
{
cmbdefaultPrinter.Properties.Items.Add(p);
}
}
//Here cmbdefaultPrinter is a combobox, you can fill the values into a list.
Namespaces required for the above code are:
using System.Drawing.Printing;
using System.Runtime.InteropServices;

Muhammad Abbas
- 389
- 6
- 15
0
This is the method I use now. I include the SetDefaultPrinter method to improve reliability. I derived this from other answers, and it is revised to reflect feedback for my previous answer version.
Regarding comments: This method is appropriate only inside the scope of a running C# application session. This method does not change printer settings managed by the operating system or stored in the registry.
using System.Drawing.Printing;
[DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool SetDefaultPrinter(string Name);
public static PrinterSettings Printer_Settings = new System.Drawing.Printing.PrinterSettings();
/// <summary>
/// Get or Sets the session's Default Printer
/// </summary>
public static string Session_DefaultPrinter
{
get { return Printer_Settings.PrinterName; }
set
{
SetDefaultPrinter(value);
Printer_Settings.DefaultPageSettings.PrinterSettings.PrinterName = value;
Printer_Settings.PrinterName = value;
}
}
Typical Usage:
string stashPrinterName = Session_DefaultPrinter;
// Switch to my Special Printer
Session_DefaultPrinter = mySpecialPrinter;
// print to my Special Printer
// ...
// Restore the original session's printer
Session_DefaultPrinter = stashPrinterName;

gridtrak
- 731
- 7
- 20
-
Nope, it doesnt change system's default printer. – AgentFire Jul 14 '20 at 21:43
-
[@AgentFire](https://stackoverflow.com/users/558018/agentfire), you are correct! I revised the routine to be more reliable in my application and updated my posting accordingly. Thank you for your feedback! It really helps! – gridtrak Aug 19 '20 at 04:12
-
I have already implemented my version of this - using the registry. I still don't think your code would change default printer setting for the Local System account. – AgentFire Aug 19 '20 at 05:52
-
@AgentFire, you are correct! Feel free to provide a link to it. I added clarification. The method I describe only changes the printer within the application scope. My applications typically run in remote desktop environments where staying within the application scope is preferable to making System changes that possibly become obsolete to future sessions. Changing and permanently saving the Operating System's managed printer settings, typically stored in the registry, that would affect other applications is not within the scope of my posting. – gridtrak Aug 31 '20 at 20:50