WMI Printer Status never changing

dwains

Member
Joined
Feb 23, 2016
Messages
14
Programming Experience
3-5
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
Code:
' 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
 

dwains

Member
Joined
Feb 23, 2016
Messages
14
Programming Experience
3-5
John all printerstaus = 3 (idle) on or off This is not a network printer even looked at the extended status with that being said is there any try catch statements I could use if the printer errors out for any reason?
 

dwains

Member
Joined
Feb 23, 2016
Messages
14
Programming Experience
3-5
Oddly there is nothing reported on Availability. By the way the app is cool there does seem to be a problem though in picture you see properties with arrays and datatypes are not listed?
see image below for exact phrase
Availability.PNG
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,291
Location
Norway
Programming Experience
10+
No, I didn't mean "search for property values", just select Availability (and f.ex Name) and run the code to see what the printers report.
"search for property values" is used to build a filter expression for specific property values.
 
Top Bottom