在大中型公司,在任何给定时间里,都可能发生由于卡纸、纸张用完或某些其他有问题而导致多台打印机无法工作的情况。 Microsoft .NET 框架的 Api 中公开的一组丰富的打印机属性提供了一种快速调查打印机状态的方法。

示例

以下是创建此类实用程序的主要步骤。
获取所有打印服务器的列表。
循环访问服务器以查询其打印队列。
在每一轮服务器循环访问过程中,循环访问所有服务器的队列并读取每个属性,这些属性可能指示队列当前不在工作。
以下代码是一系列代码段。 为简单起见,本示例假定存在通过 CRLF 分隔的打印服务器列表。 变量fileOfPrintServers是StreamReader 此文件的对象。 由于每个服务器名称都在其自己的行上,因此的任何调用都将ReadLine 获取下一个服务器的名称,并将StreamReader 的光标移到下一行的开头。
在外部循环中,代码PrintServer 为最新的打印服务器创建一个对象,并指定该应用程序将具有对服务器的管理权限。
备注
然后,该示例使用GetPrintQueues 创建服务器的所有队列的集合,并开始循环遍历它们。 此内部循环包含一个分支结构,该结构对应于检查打印机状态的两种方法:
此示例演示了这两种方法,因此,如果用户想要使用属性的标志,则以前会提示用户要使用哪种方法并使用 "y" 做出响应QueueStatus 。 请参阅以下有关这两种方法的详细信息。
最后,会向用户显示结果。
// Survey queue status for every queue on every print server System::String^ line; System::String^ statusReport = "\n\nAny problem states are indicated below:\n\n"; while ((line = fileOfPrintServers->ReadLine()) != nullptr) { System::Printing::PrintServer^ myPS = gcnew System::Printing::PrintServer(line, PrintSystemDesiredAccess::AdministrateServer); System::Printing::PrintQueueCollection^ myPrintQueues = myPS->GetPrintQueues(); statusReport = statusReport + "\n" + line; for each (System::Printing::PrintQueue^ pq in myPrintQueues) { pq->Refresh(); statusReport = statusReport + "\n\t" + pq->Name + ":"; if (useAttributesResponse == "y") { TroubleSpotter::SpotTroubleUsingQueueAttributes(statusReport, pq); // TroubleSpotter class is defined in the complete example. } else { TroubleSpotter::SpotTroubleUsingProperties(statusReport, pq); } } } fileOfPrintServers->Close(); Console::WriteLine(statusReport); Console::WriteLine("\nPress Return to continue."); Console::ReadLine();// Survey queue status for every queue on every print server String line; String statusReport = "\n\nAny problem states are indicated below:\n\n"; while ((line = fileOfPrintServers.ReadLine()) != null) { PrintServer myPS = new PrintServer(line, PrintSystemDesiredAccess.AdministrateServer); PrintQueueCollection myPrintQueues = myPS.GetPrintQueues(); statusReport = statusReport + "\n" + line; foreach (PrintQueue pq in myPrintQueues) { pq.Refresh(); statusReport = statusReport + "\n\t" + pq.Name + ":"; if (useAttributesResponse == "y") { TroubleSpotter.SpotTroubleUsingQueueAttributes(ref statusReport, pq); // TroubleSpotter class is defined in the complete example. } else { TroubleSpotter.SpotTroubleUsingProperties(ref statusReport, pq); } }// end for each print queue }// end while list of print servers is not yet exhausted fileOfPrintServers.Close(); Console.WriteLine(statusReport); Console.WriteLine("\nPress Return to continue."); Console.ReadLine();' Survey queue status for every queue on every print server Dim line As String Dim statusReport As String = vbLf & vbLf & "Any problem states are indicated below:" & vbLf & vbLf line = fileOfPrintServers.ReadLine() Do While line IsNot Nothing Dim myPS As New PrintServer(line, PrintSystemDesiredAccess.AdministrateServer) Dim myPrintQueues As PrintQueueCollection = myPS.GetPrintQueues() statusReport = statusReport & vbLf & line For Each pq As PrintQueue In myPrintQueues pq.Refresh() statusReport = statusReport & vbLf & vbTab & pq.Name & ":" If useAttributesResponse = "y" Then TroubleSpotter.SpotTroubleUsingQueueAttributes(statusReport, pq) ' TroubleSpotter class is defined in the complete example. Else TroubleSpotter.SpotTroubleUsingProperties(statusReport, pq) End If Next pq ' end for each print queue line = fileOfPrintServers.ReadLine() Loop ' end while list of print servers is not yet exhausted fileOfPrintServers.Close() Console.WriteLine(statusReport) Console.WriteLine(vbLf & "Press Return to continue.") Console.ReadLine()