Home All Groups Group Topic Archive Search About

Detecting if a printer is connected to the PC

Author
18 Aug 2006 3:23 AM
BobRoyAce
I have an application that prints bar code labels for each new record
added to the system. It uses a particular printer to do this (Brother
P-Touch QL500). What I'd like to do is not send the request to print
the label if the printer isn't physically connected to the PC. Is there
a way that I can detect this?

Author
18 Aug 2006 10:22 AM
Siva M
http://www.codeproject.com/csharp/printeroffline.asp

Sorry the code is in c#

"BobRoyAce" <b***@omegasoftwareinc.com> wrote in message
news:1155871406.551123.130760@74g2000cwt.googlegroups.com...
I have an application that prints bar code labels for each new record
added to the system. It uses a particular printer to do this (Brother
P-Touch QL500). What I'd like to do is not send the request to print
the label if the printer isn't physically connected to the PC. Is there
a way that I can detect this?
Author
18 Aug 2006 2:35 PM
BobRoyAce
Thanks for the pointer. I am having a little trouble converting this
code to VB in my app. The biggest problem that I am having is dealing
with the line

using System.Management;

I tried adding a line at the beginning of my module

Imports System.Management

but System.Management is not recognized. What do I do?

Lastly, I am unsure what the @ symbol in the following line is for:
  ManagementScope scope = new ManagementScope(@"\root\cimv2");

I translated it to:
  Dim scope As ManagementScope = New ManagementScope("\root\cimv2")

completing removing the @ symbol. Is this correct?

When I've got this all figured out, I'll post my working VB code.
Author
18 Aug 2006 2:54 PM
BobRoyAce
Never mind...I figured out that I just need to add a reference to my
project to System.Management!

Final VB code follows:

  Public Function PrinterIsOnline(ByVal sPrinterName As String) As
Boolean
    '// Set management scope
    Dim scope As ManagementScope = New ManagementScope("\root\cimv2")
    scope.Connect()

    '// Select Printers from WMI Object Collections
    Dim searcher As ManagementObjectSearcher = New
ManagementObjectSearcher("SELECT * FROM Win32_Printer")

    Dim printerName As String = String.Empty
    For Each printer As ManagementObject In searcher.Get()
      printerName = printer("Name").ToString().ToLower()
      Debug.Print(printerName)
      If (printerName.Equals(sPrinterName)) Then
        If (printer("WorkOffline").ToString().ToLower().Equals("true"))
Then
          ' Printer is offline by user
          Return False
        Else
          ' Printer is not offline
          Return True
        End If
      End If
    Next
  End Function   ' PrinterIsOnline