Solved

User Friendly Printer Name

Posted on 2011-02-27
3
375 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Visual Studio installation 5 39
Total in textboxes 9 35
Code enhancement 5 13
Events in static methods 3 27
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

759 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now