Is there an easy way to change the default printer from .net?



Is there an easy way to change the default printer from .net?



Chris Taylor


There are three options I am aware of..
1) Using WMI and invoking the SetDefaultPrinter method on the Win32_Printer
2) Interop and call the Win32 API function SetDefaultPrinter
3) Manipulate the registry directly and sent a broadcast message to all

In my opinion option one is the cleanest option from .NET followed by option
2 and option 3 is a last resort in the case where you need to support OS
prior to Windows 2000.

The following example will enumerate the printers using WMI and invoke the
SetDefaultPrinter method of each of the printers. For this you will need to
add a reference to the System.Management assembly.

System.Management.ManagementObjectSearcher search;
System.Management.ManagementObjectCollection results;

search = new System.Management.ManagementObjectSearcher("select *
from win32_printer");
results = search.Get();

foreach (System.Management.ManagementObject printer in results)
printer.InvokeMethod("SetDefaultPrinter", null);

If you need more info on the other options, just post here.

Hope this helps

Chris Taylor

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question