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

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

示例

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

然后,该代码会创建一个 LocalPrintServer 对象,这是一个派生自 PrintServer 的类。 本地打印服务器是运行应用程序的计算机。

最后一个重要步骤是将数组传递给 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 方法来筛选出不支持双面打印的打印机,并测试返回值是否存在双面打印。

另请参阅