Changing Printers Programmatically

103 views
Skip to first unread message

Vulgrin

unread,
Dec 30, 2004, 12:48:57 AM12/30/04
to DotNetDe...@googlegroups.com
I really must be missing something. I haven't ever built printer
functions into a .NET app before, but surely there is a quick and easy
way to change the printer from the default printer before starting a
print job with PrintDocument. The only references I'm seeing on the
web about this is to use the PrintDialog, which I can't do because this
is a non-user interfacing application. But I can't fathom that the
only way to do this is call the API.

What am I missing?

gav

unread,
Dec 30, 2004, 3:46:07 AM12/30/04
to DotNetDe...@googlegroups.com
you could look into WMI - its a technology that allows you to
view/modify computer configuration, through WMI you can reboot a users
machine, search the registry etc... have a look at the
"SetDefaultPrinter" Method of the "Win32_Printer" Class

its a bit of a pain trying to execute wmi methods through .NET, the
syntax is less than obvious and the help is almost non-existent...so it
will be something like thsi (not exact, this is modified from my own
code and thus will not be perfect - cos it'll be executing a diff
method)

Good luck
------------------------------------------------------------------------------------------------------------------------------------------------------------------------


using System.Management;


//connect to the comp
string strComputerName = Environment.MachineName;
ConnectionOptions oConn = new ConnectionOptions();
oConn.Impersonation = ImpersonationLevel.Impersonate;
oConn.Timeout = new TimeSpan(0,0,0,5);

oMs = new ManagementScope(@"\\" + strComputerName + @"\root\cimv2",
oConn);
oMs.Connect();


//this executes the Create method of Win32_ScheduledJob
string stringCommandLine = strSharedPath + @"\" + strComputerName +
".hta ";// + nTestCounter.ToString();

ManagementPath myPath = new ManagementPath("Win32_ScheduledJob");
ManagementClass myTaskClass = new ManagementClass(oMs,myPath,null);
DateTime dtNow = DateTime.Now;
dtNow = dtNow.AddMinutes(1);
string LocalDateTime = ToDMTFTime(dtNow);

ManagementBaseObject inputArgs =
myTaskClass.GetMethodParameters("Create");
inputArgs["Command"] = stringCommandLine;
inputArgs["DaysOfMonth"] = 0;
inputArgs["DaysOfWeek"] = 0;
inputArgs["InteractWithDesktop"] = true;
inputArgs["RunRepeatedly"] = false;
inputArgs["StartTime"] = LocalDateTime;

ManagementBaseObject outParams = myTaskClass.InvokeMethod("Create",
inputArgs, null);

Vulgrin

unread,
Dec 31, 2004, 10:59:17 AM12/31/04
to DotNetDe...@googlegroups.com
I had seen another post / article about WMI, I think on codeproject.
Thanks, I'll probably go this route - though it effectively single
threads my print service I'm trying to write. Probably not a big deal,
but if Microsoft is listening:

"This Sucks. It seems like the most obvious thing that when you want
to print, you would like to programmatically set a default printer.
You can't always leave it up to the user, and I shouldn't have to go to
the WinAPI or WMI to work around it..."

<stepping off soapbox> :)

Vulgrin

unread,
Dec 31, 2004, 3:58:35 PM12/31/04
to DotNetDe...@googlegroups.com
Well, let me catch myself before someone comes and makes me a bigger
fool than I was. :) Apparently PrinterSettings.PrinterName IS read /
write... I must have needed more sleep. sigh.

Will report back if it doesn't work... otherwise, I'll be off in the
corner hitting myself in the head with a 2x4.

Aqeel Abbas

unread,
Jan 14, 2005, 5:19:12 AM1/14/05
to DotNetDe...@googlegroups.com
hi, vulgrin

may be i m late but it will help u.

Public g_PrinterA As String
Public g_PrinterB As String
Public g_PrinterC As String

g_PrinterA = "HP"
g_PrinterB= "Epson"
g_PrinterC= "Cannon"

Select Case g_Option
Case Is = "Laser"
PrintDocument1.PrinterSettings.PrinterName = g_PrinterA
Case Is = "DotMatix"
PrintDocument1.PrinterSettings.PrinterName = g_PrinterB
Case Is = "InkJet"
PrintDocument1.PrinterSettings.PrinterName = g_PrinterC
End Select

PrintDocument1.PrintController =New
System.Drawing.Printing.StandardPrintController()
PrintDocument1.Print()

PrintDocument1.Print()

aqeel.
Reply all
Reply to author
Forward
0 new messages