如何:列舉列印佇列的子集
管理全公司印表機的資訊技術 (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 方法,並測試傳回的值是否有雙工,以篩選出不支援雙面列印的印表機。