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

更新:2007 年 11 月

负责管理公司范围内打印机的信息技术 (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
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
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();

可以通过让单步执行每个打印队列的 foreach 循环执行进一步筛选来扩展此示例。例如,可以通过让该循环调用每个打印队列的 GetPrintCapabilities 方法并对双面打印存在与否的返回值进行测试,以便筛选掉不支持双面打印的打印机。有关示例,请参见 创建 XPS 文档示例

请参见

概念

Windows Presentation Foundation 中的文档

打印概述

参考

GetPrintQueues

PrintServer

LocalPrintServer

EnumeratedPrintQueueTypes

PrintQueue

GetPrintCapabilities

其他资源

打印示例

Microsoft XPS Document Writer