共用方式為


如何:複製印表機

大部分企業有時候會購買相同型號的多部印表機。 一般而言,這些印表機全都已安裝幾乎完全相同的組態設定。 安裝每部印表機可能非常耗時且容易出錯。 System.Printing.IndexedProperties 命名空間和以 Microsoft .NET Framework 公開的 InstallPrintQueue 類別,可讓您立即安裝從現有列印佇列複製的任意數目額外列印佇列。

範例

在下列範例中,會從現有列印佇列複製第二個列印佇列。 第二個僅有名稱、位置、連接埠和共用狀態與第一個不同。 執行這項操作的主要步驟如下。

  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 以新值安裝新的印表機。

範例如下。

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();
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()

另請參閱