Avatar of rturney
rturney
Flag 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
C#

Avatar of undefined
Last Comment
horne0268

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Bob Learned

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Bob Learned

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

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
Bob Learned

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
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
rturney

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

Thanks,
-Bob
rturney

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

Best regards,
-Bob
horne0268

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?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.