Results 1 to 6 of 6

Thread: WMI Printer Status never changing

  1. #1
    dwains is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Feb 2016
    Posts
    14
    Reputation
    17

    WMI Printer Status never changing

    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

  2. #2
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    15,090
    Reputation
    2804
    Give Download WMI Code Creator v1.0 from Official Microsoft Download Center a test run, to see if you get same results that your code does.
    What I read is printer reporting can be unreliable (or not updated), another option for network printers it is to use SNMP if supported.
    How to format posts with code blocks etc - present the problem/post properly

    Visual Studio Community 2017

  3. #3
    dwains is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Feb 2016
    Posts
    14
    Reputation
    17
    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?

  4. #4
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    15,090
    Reputation
    2804
    How's the Availability property? That looks like related to power status.
    How to format posts with code blocks etc - present the problem/post properly

    Visual Studio Community 2017

  5. #5
    dwains is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Feb 2016
    Posts
    14
    Reputation
    17
    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

  6. #6
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    15,090
    Reputation
    2804
    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.
    How to format posts with code blocks etc - present the problem/post properly

    Visual Studio Community 2017

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •