How do I set the windows default printer in C#?
43,530
Solution 1
Using the SetDefaultPrinter Windows API.
Solution 2
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;
Author by
jms
Updated on August 08, 2022Comments
-
jms almost 2 years
How do I set the windows default printer in C#.NET?