Set Default Printer

G

Guest

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
 
E

Eric Moreau

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.Network")

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)
 
G

Guest

Eric:

Thank you

Eric Moreau said:
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.Network")

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 said:
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
 

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

Top