Solved

User Friendly Printer Name

Posted on 2011-02-27
3
378 Views
Last Modified: 2012-05-11
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
Comment
Question by:dastaub
  • 2
3 Comments
 
LVL 12

Expert Comment

by:omegaomega
ID: 34993725
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
 
LVL 12

Accepted Solution

by:
omegaomega earned 500 total points
ID: 34993729
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
 

Author Closing Comment

by:dastaub
ID: 34993926
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question