共用方式為


HOW TO:複製印表機

大部分公司有時會購買多部相同機型的印表機。 一般而言,這些都會使用虛擬上相同的組態設定來進行安裝。 而安裝每部印表機十分耗時而且容易產生錯誤。 使用 Microsoft .NET Framework 公開的 System.Printing.IndexedProperties 命名空間和 InstallPrintQueue 類別,可以立即安裝從現有列印佇列複製 (Clone) 的任何數目的額外列印佇列。

範例

在下面範例中,第二個列印佇列是從現有列印佇列複製而來。 第二個列印佇列與第一個列印佇列的差別只有在於名稱、位置、連接埠和共用狀態。 這個作業的主要步驟如下:

  1. 建立要複製之現有印表機的 PrintQueue 物件。

  2. PrintQueuePropertiesCollection 建立 PrintPropertyDictionary。 在這個字典中,每個項目的 Value 屬性各代表從 PrintProperty 衍生的其中一個型別的物件。 有兩種方式可以在這個字典中設定項目值。

    • 使用字典的 RemoveAdd 方法移除項目,然後使用所要的值重新加入項目。

    • 使用字典的 SetProperty 方法。

    下列範例會示範這兩種方式。

  3. 建立 PrintBooleanProperty 物件,並將它的 Name 設定為 "IsShared",以及將它的 Value 設定為 true。

  4. 使用 PrintBooleanProperty 物件做為 PrintPropertyDictionary 的 "IsShared" 項目值。

  5. 建立 PrintStringProperty 物件,並將它的 Name 設定為 "ShareName",以及將它的 Value 設定為適當 String

  6. 使用 PrintStringProperty 物件做為 PrintPropertyDictionary 的 "ShareName" 項目值。

  7. 建立另一個 PrintStringProperty 物件,並將它的 Name 設定為 "Location",以及將它的 Value 設定為適當 String

  8. 使用第二個 PrintStringProperty 物件做為 PrintPropertyDictionary 的 "Location" 項目值。

  9. 建立 String 的陣列。 每個項目都是伺服器上的連接埠名稱。

  10. 使用 InstallPrintQueue,以利用新的值安裝新的印表機。

範例如下。

                Dim myLocalPrintServer As New LocalPrintServer(PrintSystemDesiredAccess.AdministrateServer)
                Dim sourcePrintQueue As PrintQueue = myLocalPrintServer.DefaultPrintQueue
                Dim myPrintProperties As PrintPropertyDictionary = sourcePrintQueue.PropertiesCollection

                ' Share the new printer using Remove/Add methods
                Dim [shared] As New PrintBooleanProperty("IsShared", True)
                myPrintProperties.Remove("IsShared")
                myPrintProperties.Add("IsShared", [shared])

                ' Give the new printer its share name using SetProperty method
                Dim theShareName As New PrintStringProperty("ShareName", """Son of " & sourcePrintQueue.Name & """")
                myPrintProperties.SetProperty("ShareName", theShareName)

                ' Specify the physical location of the new printer using Remove/Add methods
                Dim theLocation As New PrintStringProperty("Location", "the supply room")
                myPrintProperties.Remove("Location")
                myPrintProperties.Add("Location", theLocation)

                ' Specify the port for the new printer
                Dim port() As String = { "COM1:" }


                ' Install the new printer on the local print server
                Dim clonedPrinter As PrintQueue = myLocalPrintServer.InstallPrintQueue("My clone of " & sourcePrintQueue.Name, "Xerox WCP 35 PS", port, "WinPrint", myPrintProperties)
                myLocalPrintServer.Commit()

                ' Report outcome
                Console.WriteLine("{0} in {1} has been installed and shared as {2}", clonedPrinter.Name, clonedPrinter.Location, clonedPrinter.ShareName)
                Console.WriteLine("Press Return to continue ...")
                Console.ReadLine()
LocalPrintServer myLocalPrintServer = new LocalPrintServer(PrintSystemDesiredAccess.AdministrateServer);
PrintQueue sourcePrintQueue = myLocalPrintServer.DefaultPrintQueue;
PrintPropertyDictionary myPrintProperties = sourcePrintQueue.PropertiesCollection;

// Share the new printer using Remove/Add methods
PrintBooleanProperty shared = new PrintBooleanProperty("IsShared", true);
myPrintProperties.Remove("IsShared");
myPrintProperties.Add("IsShared", shared);

// Give the new printer its share name using SetProperty method
PrintStringProperty theShareName = new PrintStringProperty("ShareName", "\"Son of " + sourcePrintQueue.Name +"\"");
myPrintProperties.SetProperty("ShareName", theShareName);

// Specify the physical location of the new printer using Remove/Add methods
PrintStringProperty theLocation = new PrintStringProperty("Location", "the supply room");
myPrintProperties.Remove("Location");
myPrintProperties.Add("Location", theLocation);

// Specify the port for the new printer
String[] port = new String[] { "COM1:" };


// Install the new printer on the local print server
PrintQueue clonedPrinter = myLocalPrintServer.InstallPrintQueue("My clone of " + sourcePrintQueue.Name, "Xerox WCP 35 PS", port, "WinPrint", myPrintProperties);
myLocalPrintServer.Commit();

// Report outcome
Console.WriteLine("{0} in {1} has been installed and shared as {2}", clonedPrinter.Name, clonedPrinter.Location, clonedPrinter.ShareName);
Console.WriteLine("Press Return to continue ...");
Console.ReadLine();

請參閱

參考

System.Printing.IndexedProperties

PrintPropertyDictionary

LocalPrintServer

PrintQueue

DictionaryEntry

概念

WPF 中的文件

列印概觀