Link to home
Start Free TrialLog in
Avatar of dastaub
dastaubFlag for United States of America

asked on

User Friendly Printer Name

The below loop populates the listbox with all available printers.  The problem is the printers are listed as \\DELL09\HP Laser Jet 5050ii instead of "Front Desk Laser"  Is there a way to tap the location or comment field associated with the network printers?  The end user will understand those choices better.
        Dim mPrinterNames As String
        ListBox1.Items.Clear()
        For Each mPrinterNames In System.Drawing.Printing.PrinterSettings.InstalledPrinters
            ListBox1.Items.Add(mPrinterNames)
        Next mPrinterNames
Avatar of omegaomega
omegaomega
Flag of Canada image

Hello, dastaub,

You can use the ManagementClass to do this.  The snippet contains an example.  In the example, I have used the Comment, Location and Name properties in that order of preference.  Of course, these (and other properties) could be combined to give a more complete description in the ListBox.

Note that you need to add a reference to System.Management.dll to your project to use this.

Cheers,
Randy

ListBox1.Items.Clear()
        Dim mgcPrinters As New ManagementClass("Win32_Printer")
        For Each mgoPrinter As ManagementObject In mgcPrinters.GetInstances()
            Dim strPrinterTitle As String = CStr(mgoPrinter.Properties("Comment").Value)
            If (strPrinterTitle = "") Then  ' If no comment provided, then use printer's location.
                strPrinterTitle = CStr(mgoPrinter.Properties("Location").Value)
            End If
            If (strPrinterTitle = "") Then                  ' If no comment or location 
                strPrinterTitle = CStr(mgoPrinter("Name"))  ' provided, then default to 
            End If                                          ' the printer's name.
            ListBox1.Items.Add(strPrinterTitle)
        Next mgoPrinter

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of omegaomega
omegaomega
Flag of Canada 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
Avatar of dastaub

ASKER

Thank You.  There always appears to be a class to access the property, method, or event that you want to get to.