开发者

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开发者_如何学Python 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

  • \\local (Local computer with 3 print queues defined, 1 is a remote connection)
    • LPrinter1
    • LPrinter2
    • \\remote\RPrinter1
  • \\remote (Remote computer with 2 print queues defined)

    • RPrinter1
    • RPrinter2
  • \\other (Some other computer with 1 print queue defined)
    • OPrinter

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.


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


Private Sub btnreanudar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnreanudar.Click
    Dim SERVIDOR As New System.Printing.PrintServer() 'SUPERIOR DEL SISTEMA DE IMPRESION (EL ORDENADOR)
    Dim IMPRESORAS As PrintQueueCollection = SERVIDOR.GetPrintQueues() 'IMPRESORAS DISPONIBLES
    For Each IMPRESORA As PrintQueue In IMPRESORAS 'RECORRE TODAS LAS IMPRESORAS
        Try
            If IMPRESORA.NumberOfJobs > 0 Then 'SI LA IMPRESORA TIENE ALGUNA IMPRESION EN MARCHA......
                IMPRESORA.Refresh()
                Dim IMPRESIONES As PrintJobInfoCollection = IMPRESORA.GetPrintJobInfoCollection() 'CREA UNA COLECCION DE IMPRESIONES EN MARCHA
                For Each IMPRESION In IMPRESIONES ' POR CADA IMPRESION......
                    If IMPRESION.JobIdentifier = joblist.CurrentRow.Cells("JobId").Value Then
                        IMPRESION.Resume()
                        Exit Sub
                    End If
                Next
            End If
        Catch ex As Exception
        End Try
    Next
End Sub
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜