方法 : プリンターを複製する

ほとんどの企業は、ある時点で、同じモデルのプリンターを複数購入します。 通常、これらのプリンターはすべて、ほぼ同一の構成設定でインストールされます。 プリンターを個別にインストールすると、時間がかかったりエラーが発生したりすることがあります。 Microsoft .NET Framework で公開されている System.Printing.IndexedProperties 名前空間と InstallPrintQueue クラスを使用すると、既存の印刷キューから複製した任意の数の追加印刷キューを直ちにインストールすることができます。

使用例

次の例の 2 番目の印刷キューは、既存の印刷キューから複製したものです。 2 番目の印刷キューと最初の印刷キューの違いは、名前、場所、ポート、および共有状態だけです。 主な実行手順は次のとおりです。

  1. 複製する既存のプリンターの PrintQueue オブジェクトを作成します。

  2. PrintQueuePropertiesCollection から PrintPropertyDictionary を作成します。 このディクショナリの各エントリの Value プロパティは、PrintProperty から派生した型の 1 つのオブジェクトです。 このディクショナリのエントリの値を設定するには、2 つの方法があります。

    • ディクショナリの Remove メソッドと Add メソッドを使用してエントリを削除し、目的の値を追加し直します。

    • ディクショナリの SetProperty メソッドを使用します。

    両方の方法を次の例に示します。

  3. PrintBooleanProperty オブジェクトを作成し、その Name を "IsShared" に、Value を true にそれぞれ設定します。

  4. PrintBooleanProperty オブジェクトを PrintPropertyDictionary の "IsShared" エントリの値として使用します。

  5. PrintStringProperty オブジェクトを作成し、その Name を "ShareName" に、Value を適切な String にそれぞれ設定します。

  6. PrintStringProperty オブジェクトを PrintPropertyDictionary の "ShareName" エントリの値として使用します。

  7. PrintStringProperty オブジェクトをもう 1 つ作成し、その Name を "Location" に、Value を適切な String にそれぞれ設定します。

  8. 2 番目の 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 のドキュメント

印刷の概要