Solved

User Friendly Printer Name

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

808 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