.netsystem.printing

How to get System.Printing.PrintServer.GetPrintQueues to return the print queue list from remote server?


Problem

I'm trying to get a list of print queues available on a remote server.

Ultimately this will need to be performed from ASP.NET, but for now I'd settle for a console application to work.

When I create an instance of the System.Printing.PrintServer class using the path to a remote server I am able to get basic information about the Print Server. But when I call the GetPrintQueues method I only get queues that are defined on the local box. No matter what I use for the remote device.

Code

Imports System.Printing

Module Module1
  Sub Main()
    ListPrintQueues("\\local")
    ListPrintQueues("\\remote")
    ListPrintQueues("\\other")
  End Sub

  Sub ListPrintQueues(ByVal server As String)

    Dim ps As New PrintServer(server)
    Console.WriteLine("Printer Server=" & ps.Name)

    Dim flags() As EnumeratedPrintQueueTypes = {EnumeratedPrintQueueTypes.Connections, EnumeratedPrintQueueTypes.Local}

    Dim queues As PrintQueueCollection = ps.GetPrintQueues(flags)

    For Each pq As PrintQueue In queues
      Console.WriteLine(pq.FullName)
    Next

    Console.WriteLine()
  End Sub
End Module

Example:

Assuming the following configuration

The results are:

Print Server=\\local  
\\local\LPrinter1  
\\local\LPrinter2  
\\remote\RPrinter1  

Print Server=\\remote  
\\remote\RPrinter1  

Print Server=\\other
\\remote\RPrinter1  

My best guess is that something is happening inside the GetPrintQueues() method to cause the print server to be reset to the local box since it doesn't matter what the print server name is as long as it's a valid computer on the network.


Solution

  • Discovered an answer...even if it's not what I wanted.

    If I change the enumeration flags to local only and connect to a server that I have logged on to in the past I will get the correct printers. But if I have not logged on then I get list of remote print queues from my machine.

    When I attempt similar actions using WMI I get Access Denied errors on the remote server that I've connected to. My guess is that System.Printing is catching the exception then defaulting to the local print server.

    changed code

    Imports System.Printing
    
    Module Module1
      Sub Main()
        ListPrintQueues("\\local")
        ListPrintQueues("\\remote")
        ListPrintQueues("\\other")
      End Sub
    
      Sub ListPrintQueues(ByVal server As String)
    
        Dim ps As New PrintServer(server)
        Console.WriteLine("Printer Server=" & ps.Name)
    
        Dim flags() As EnumeratedPrintQueueTypes = {EnumeratedPrintQueueTypes.Local}
    
        Dim queues As PrintQueueCollection = ps.GetPrintQueues(flags)
    
        For Each pq As PrintQueue In queues
          Console.WriteLine(pq.FullName)
        Next
    
        Console.WriteLine()
      End Sub
    End Module