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

T

ThunderMusic

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

thanks

ThunderMusic
 
C

Chris Taylor

Hi,

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

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)
{
System.Diagnostics.Debug.WriteLine(printer["Name"]);
printer.InvokeMethod("SetDefaultPrinter", null);
}

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

Hope this helps

Chris Taylor
http://dotnetjunkies.com/weblog/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

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top