I use this code in VB.Net. You shouldn't too much problem converting it:
Dim strOldPrinter As String
Dim WshNetwork As Object
Dim pd As New PrintDocument
Try
strOldPrinter = pd.PrinterSettings.PrinterName
WshNetwork = Microsoft.VisualBasic.CreateObject("WScript.Networ k")
WshNetwork.SetDefaultPrinter(strPrinterName)
pd.PrinterSettings.PrinterName = strPrinterName
If pd.PrinterSettings.IsValid Then
Return True
Else
WshNetwork.SetDefaultPrinter(strOldPrinter)
Return False
End If
Catch exptd As Exception
WshNetwork.SetDefaultPrinter(strOldPrinter)
Return False
Finally
WshNetwork = Nothing
pd = Nothing
End Try
--
HTH
Éric Moreau, MCSD, Visual Developer - Visual Basic MVP
(
http://aspnet2.com/mvp.ashx?EricMoreau)
Conseiller Principal / Senior Consultant
Concept S2i inc. (
www.s2i.com)
"Andy" <An**@discussions.microsoft.com> wrote in message
news:C0**********************************@microsof t.com...
How do you set the default printer in C#? I have added a printer and now
want to set that printer to the default. Listed below is my code, but I
am
getting an "Invalid method Parameter(s)" error on the 4th line:
ManagementClass printerClass = new ManagementClass("Win32_Printer");
object[] printerName = {"\\\\REGWPROD\\REGW0951"};
object result = printerClass.InvokeMethod("AddPrinterConnection",
printerName);
result = printerClass.InvokeMethod("SetDefaultPrinter", printerName);
Can anyone tell me what I am missing?
Thanks
Andy