Sunday, 14 October 2007

How to change default printers in .NET

Apparently, the System.Management namespace gives access to the WMI classes. However, that only gives me a configurable Win32_Printer object and a readonly Win32_PrinterConfiguration object...

There is a way around this one:

public static void SetDefaultPrinter(string name)
{
try {
Type t = Type.GetTypeFromProgID("WScript.Network");
Object o = Activator.CreateInstance(t);
t.InvokeMember("SetDefaultPrinter", System.Reflection.BindingFlags.InvokeMethod, null, o, new object[] {name});
}
catch (Exception)
{ throw new Exception("Unable to set a default printer."); }
}

1 comment:

chinaprinting said...

Technological potential, and printing desires may mean a secure profit for many companies. Printing doesn't resume to only public stores or companies, but also buying printers for your own home.