• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 383
  • Last Modified:

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
0
dastaub
Asked:
dastaub
  • 2
1 Solution
 
omegaomegaDeveloperCommented:
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

0
 
omegaomegaDeveloperCommented:
Hello, dastaub,

By the way, not shown in the snippet is the line:

    Imports System.Management

that I have included at the top of my sample code.  Here's the full code I used.

Cheers,
Randy

Imports System.Management
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        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
    End Sub

End Class

Open in new window

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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now