Detecting if a printer is connected to the PC

  • Thread starter Thread starter BobRoyAce
  • Start date Start date
B

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?
 
http://www.codeproject.com/csharp/printeroffline.asp

Sorry the code is in c#

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?
 
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.
 
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
 
Back
Top