如何让 System.Printing.PrintServer.GetPrintQueues 从远程服务器返回打印队列列表?

发布于 2024-09-29 00:00:20 字数 1576 浏览 2 评论 0原文

问题

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

最终这需要从 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 个打印队列的远程计算机
    • RPrinter1
    • RPrinter2
  • \\other(定义了 1 个打印队列的其他计算机
    • OPrinter

结果是:

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

Print Server=\\remote  
\\remote\RPrinter1  

Print Server=\\other
\\remote\RPrinter1  

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

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

  • \\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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

命硬 2024-10-06 00:00:20

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

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

当我尝试使用 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

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
迟到的我 2024-10-06 00:00:20
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
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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文