1

问题

我正在尝试获取远程服务器上可用的打印队列列表

最终这将需要从 ASP.NET 执行,但现在我会满足于控制台应用程序的工作。

当我使用远程服务器的路径创建System.Printing.PrintServer类的实例时,我能够获取有关打印服务器的基本信息。但是当我调用GetPrintQueues 方法时,我只得到在本地盒子上定义的队列。无论我使用什么远程设备。

代码

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

例子:

假设以下配置

  • \\local(定义了 3 个打印队列的本地计算机,1 个是远程连接
    • LPrinter1
    • LPrinter2
    • \\远程\RPrinter1
  • \\remote(定义了 2 个打印队列的远程计算机
    • R打印机1
    • RPrinter2
  • \\other(定义了 1 个打印队列的其他计算机
    • 打印机

结果是:

打印服务器=\\本地  
\\本地\LPrinter1  
\\本地\LPrinter2  
\\远程\RPrinter1  

打印服务器=\\远程  
\\远程\RPrinter1  

打印服务器=\\其他
\\远程\RPrinter1  

我最好的猜测是 GetPrintQueues() 方法内部发生了一些事情,导致打印服务器被重置为本地框,因为只要它是网络上的有效计算机,打印服务器名称是什么并不重要。

4

2 回答 2

1

找到了答案……即使这不是我想要的。

如果我将枚举标志更改为仅限本地连接到我过去登录过的服务器,我将获得正确的打印机。但是,如果我没有登录,那么我会从我的机器上获取远程打印队列列表。

当我尝试使用 WMI 进行类似操作时,我在连接到的远程服务器上收到拒绝访问错误。我的猜测是 System.Printing 正在捕获异常,然后默认为本地打印服务器。

更改代码

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
于 2010-10-27T20:19:25.753 回答
0
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
于 2016-06-09T16:16:35.947 回答