How do I set the windows default printer in C#?
Using the SetDefaultPrinter Windows API.
Here's how to pInvoke that.
Setting the default Printer for windows using c#
Try SetDefaultPrinter
Windows API function
using System.Runtime.InteropServices;
...
[DllImport("winspool.drv",
CharSet = CharSet.Auto,
SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern Boolean SetDefaultPrinter(String name);
...
SetDefaultPrinter(PrinterName);
see
http://msdn.microsoft.com/en-us/library/windows/desktop/dd162971(v=vs.85).aspx
http://www.pinvoke.net/default.aspx/winspool/SetDefaultPrinter.html?diff=y
What's the best way to get the default printer in .NET
The easiest way I found is to create a new PrinterSettings
object. It starts with all default values, so you can check its Name property to get the name of the default printer.
PrinterSettings
is in System.Drawing.dll in the namespace System.Drawing.Printing
.
PrinterSettings settings = new PrinterSettings();
Console.WriteLine(settings.PrinterName);
Alternatively, you could maybe use the static PrinterSettings.InstalledPrinters
method to get a list of all printer names, then set the PrinterName property and check the IsDefaultPrinter. I haven't tried this, but the documentation seems to suggest it won't work. Apparently IsDefaultPrinter is only true when PrinterName is not explicitly set.
How to set default Printer dynamically in wpf
Try this it will help you.
var query = new ManagementObjectSearcher("SELECT * FROM Win32_Printer");
var printers = query.Get();
foreach (ManagementObject printer in printers)
{
if (printer["name"].ToString() == combox_pinter.SelectedItem.ToString())
{
printer.InvokeMethod("SetDefaultPrinter", new object[] { combox_pinter.SelectedItem.ToString() });
}
}
Let Windows manage my default printer is missing
How to disable automatic default printer manager
1- Use the Windows Key + I keyboard shortcut to open Settings.
2- Navigate to Devices, then go to Printers & scanners, and disable the Let Windows manage my default printer.
Related Topics
How to Suppress a Stylecop Warning
Possible Pitfalls of Using This (Extension Method Based) Shorthand
Run .Exe Executable File in Azure Function
Finding a Subsequence in Longer Sequence
Why Are Const Parameters Not Allowed in C#
How to Dynamic New Anonymous Class
Right Way to Dispose Image/Bitmap and Picturebox
C# Instantiate Generic List from Reflected Type
Compile-Time and Runtime Casting C#
Selectively Use Default JSON Converter
Exception Using Copytodatatable with "New {..}" Linq Query