如何:枚举打印队列的子集

信息技术(IT)专业人员面临的一种常见情况是,管理公司范围的打印机集,即生成具有特定特征的打印机列表。 由PrintServer对象的方法GetPrintQueuesEnumeratedPrintQueueTypes枚举提供此功能。

示例:

在下面的示例中,代码首先创建一个标志数组,用于指定要列出的打印队列的特征。 在此示例中,我们查找打印服务器上本地安装的打印队列,并共享。 枚举 EnumeratedPrintQueueTypes 提供了许多其他可能性。

代码然后创建一个从 PrintServer 派生而来的 LocalPrintServer 对象。 本地打印服务器是运行应用程序的计算机。

最后一个重要步骤是将数组传递给 GetPrintQueues 方法。

最后,结果将呈现给用户。

// Specify that the list will contain only the print queues that are installed as local and are shared
array<System::Printing::EnumeratedPrintQueueTypes>^ enumerationFlags = {EnumeratedPrintQueueTypes::Local,EnumeratedPrintQueueTypes::Shared};

LocalPrintServer^ printServer = gcnew LocalPrintServer();

//Use the enumerationFlags to filter out unwanted print queues
PrintQueueCollection^ printQueuesOnLocalServer = printServer->GetPrintQueues(enumerationFlags);

Console::WriteLine("These are your shared, local print queues:\n\n");

for each (PrintQueue^ printer in printQueuesOnLocalServer)
{
   Console::WriteLine("\tThe shared printer " + printer->Name + " is located at " + printer->Location + "\n");
}
Console::WriteLine("Press enter to continue.");
Console::ReadLine();
// Specify that the list will contain only the print queues that are installed as local and are shared
EnumeratedPrintQueueTypes[] enumerationFlags = {EnumeratedPrintQueueTypes.Local,
                                                EnumeratedPrintQueueTypes.Shared};

LocalPrintServer printServer = new LocalPrintServer();

//Use the enumerationFlags to filter out unwanted print queues
PrintQueueCollection printQueuesOnLocalServer = printServer.GetPrintQueues(enumerationFlags);

Console.WriteLine("These are your shared, local print queues:\n\n");

foreach (PrintQueue printer in printQueuesOnLocalServer)
{
    Console.WriteLine("\tThe shared printer " + printer.Name + " is located at " + printer.Location + "\n");
}
Console.WriteLine("Press enter to continue.");
Console.ReadLine();
' Specify that the list will contain only the print queues that are installed as local and are shared
Dim enumerationFlags() As EnumeratedPrintQueueTypes = {EnumeratedPrintQueueTypes.Local, EnumeratedPrintQueueTypes.Shared}

Dim printServer As New LocalPrintServer()

'Use the enumerationFlags to filter out unwanted print queues
Dim printQueuesOnLocalServer As PrintQueueCollection = printServer.GetPrintQueues(enumerationFlags)

Console.WriteLine("These are your shared, local print queues:" & vbLf & vbLf)

For Each printer As PrintQueue In printQueuesOnLocalServer
    Console.WriteLine(vbTab & "The shared printer " & printer.Name & " is located at " & printer.Location & vbLf)
Next printer
Console.WriteLine("Press enter to continue.")
Console.ReadLine()

可以通过让遍历每个打印队列的 foreach 循环进一步筛选来扩展此示例。 例如,可以通过循环调用每个打印队列的 GetPrintCapabilities 方法,并测试返回值是否支持双面打印,从而筛选出不支持双面打印的打印机。

另请参阅