Link to home
Start Free TrialLog in
Avatar of rturney
rturneyFlag for United States of America

asked on

How to get printer driver name and port in C#

I am converting a large program from VB6 to C#.  One of the problems I've run into is the VB6 program uses the old Kodak Imaging Control.  I've not been able to find anything inexpensive to replace it with so I have got it running in the C# program.  The problem is that the print method for the Kodak image control requires the printer name, driver and port.  This was not much of a problem in VB6 and I used:

Private Sub cboPrinters_Click()
    With frmEView
        Set Printer = Printers(cboPrinters.ListIndex)
        .gstrPrnName = Printer.DeviceName
        .gstrPrnDriver = Printer.DriverName
        .gstrPrnPort = Printer.Port
    End With
End Sub

This used the Printers collection in VB6.  Does anyone know what needs to be done in C# to reveal the printer info.  Most of the printers are network printers and usually one local one.

Thanks,
-Bob
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sample usage:

            List<Win32_Printer> printerList = Win32_Printer.GetList();
 
            foreach (Win32_Printer printer in printerList)
            {
                string name = printer.Name;
                string port = printer.PortName;
                string driverName = printer.DriverName;
            }

Open in new window

Avatar of rturney

ASKER

Thank you very much for the detailed response.  It appears that this will do what I need.  I'll test it in my code and reply back asap.

-Bob
BTW, that is only a subset of the full properties defined for the Win32_Printer class.  Plus, I missed the comments at the top:

// Add a reference to System.Management.dll to the project.
//
// MSDN reference:
// http://msdn2.microsoft.com/en-us/library/aa394363(VS.85).aspx

Bob
Avatar of rturney

ASKER

Yippeee! Your solution was just what I needed!  And the detail you provided made my job very easy.

Thanks,
-Bob
Avatar of rturney

ASKER

Thank you very much for the fantastic response to my problem!  I am very grateful!

Best regards,
-Bob
Thanks TheLearnedOne: for your posting, you just saved my head from being banged against the wall!

Just one thing though, can you think of a reason why the printer status on all my installed printrs are showing a PrinterStatus='Unknown' but when viewed in control panel | printers show as Offline or Ready?