I am trying to use WMI to get the printers status using the following code. I have researched WMI for the answer but cant find a solution the ultimate goal here is to find out if the printer is online and ready to print. I also have tried the printDocument.PrinterSettings.IsValid but it always shows true whether the printer is on or off so I assume it only validates the printers name. In the following code it lists the printers in a combo box and when the printer is selected it get the properties.
Ironically if you look at the end of the code I list all properties and values available in a listbox and none give me the answer I need. What am I missing here? Thanks Dwain
Ironically if you look at the end of the code I list all properties and values available in a listbox and none give me the answer I need. What am I missing here? Thanks Dwain
VB.NET:
' Display information about the selected printer.
Private Sub cboPrinters_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboPrinters.SelectedIndexChanged
' Lookup arrays.
Dim PrinterStatuses As String() = {"Other", "Unknown", "Idle", "Printing", "WarmUp", "Stopped Printing",
"Offline"}
Dim PrinterStates As String() = {"Paused", "Error", "Pending Deletion", "Paper Jam", "Paper Out", "Manual Feed",
"Paper Problem", "Offline", "IO Active", "Busy", "Printing", "Output Bin Full",
"Not Available", "Waiting", "Processing", "Initialization", "Warming Up", "Toner Low",
"No Toner", "Page Punt", "User Intervention Required", "Out of Memory", "Door Open", "Server_Unknown",
"Power Save"}
' Get a ManagementObjectSearcher for the printer.
Dim query As String = "SELECT * FROM Win32_Printer WHERE Name='" + cboPrinters.SelectedItem.ToString() + "'"
Dim searcher As New ManagementObjectSearcher(query)
' Get the ManagementObjectCollection representing
' the result of the WMI query. Loop through its
' single item. Display some of that item's properties.
For Each service As ManagementObject In searcher.[Get]()
txtName.Text = service.Properties("Name").Value.ToString()
Dim state As UInt32 = DirectCast(service.Properties("PrinterState").Value, UInt32)
txtState.Text = PrinterStates(state)
'The above is obsolete but always shows Paused for any selected Printer listed
Dim status As UInt16 = DirectCast(service.Properties("PrinterStatus").Value, UInt16)
txtStatus.Text = PrinterStatuses(status)
'The Above always shows printing for any selected Printer listed
' List the available properties. None of the listed properties actually will tell me if printer is online or offline
For Each data As PropertyData In service.Properties
Dim txt As String = data.Name
If data.Value IsNot Nothing Then
txt += ": " + data.Value.ToString()
End If
lstProperties.Items.Add(txt)
Next
Next
End Sub