Set Default Printer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andy

    #1

    Set Default Printer

    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.In vokeMethod("Add PrinterConnecti on",
    printerName);
    result = printerClass.In vokeMethod("Set DefaultPrinter" , printerName);

    Can anyone tell me what I am missing?

    Thanks
    Andy
  • Eric Moreau

    #2
    Re: Set Default Printer

    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.PrinterSetti ngs.PrinterName

    WshNetwork = Microsoft.Visua lBasic.CreateOb ject("WScript.N etwork")

    WshNetwork.SetD efaultPrinter(s trPrinterName)

    pd.PrinterSetti ngs.PrinterName = strPrinterName

    If pd.PrinterSetti ngs.IsValid Then

    Return True

    Else

    WshNetwork.SetD efaultPrinter(s trOldPrinter)

    Return False

    End If

    Catch exptd As Exception

    WshNetwork.SetD efaultPrinter(s trOldPrinter)

    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" <Andy@discussio ns.microsoft.co m> wrote in message
    news:C0D1071D-4029-40DE-8826-693CCA3DD260@mi crosoft.com...[color=blue]
    > 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.In vokeMethod("Add PrinterConnecti on",
    > printerName);
    > result = printerClass.In vokeMethod("Set DefaultPrinter" , printerName);
    >
    > Can anyone tell me what I am missing?
    >
    > Thanks
    > Andy[/color]


    Comment

    • Andy

      #3
      Re: Set Default Printer

      Eric:

      Thank you

      "Eric Moreau" wrote:
      [color=blue]
      > 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.PrinterSetti ngs.PrinterName
      >
      > WshNetwork = Microsoft.Visua lBasic.CreateOb ject("WScript.N etwork")
      >
      > WshNetwork.SetD efaultPrinter(s trPrinterName)
      >
      > pd.PrinterSetti ngs.PrinterName = strPrinterName
      >
      > If pd.PrinterSetti ngs.IsValid Then
      >
      > Return True
      >
      > Else
      >
      > WshNetwork.SetD efaultPrinter(s trOldPrinter)
      >
      > Return False
      >
      > End If
      >
      > Catch exptd As Exception
      >
      > WshNetwork.SetD efaultPrinter(s trOldPrinter)
      >
      > 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" <Andy@discussio ns.microsoft.co m> wrote in message
      > news:C0D1071D-4029-40DE-8826-693CCA3DD260@mi crosoft.com...[color=green]
      > > 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.In vokeMethod("Add PrinterConnecti on",
      > > printerName);
      > > result = printerClass.In vokeMethod("Set DefaultPrinter" , printerName);
      > >
      > > Can anyone tell me what I am missing?
      > >
      > > Thanks
      > > Andy[/color]
      >
      >
      >[/color]

      Comment

      Working...