如何:克隆打印机

大多数公司有时可能会购买多台同一型号的打印机。 通常,这些打印机都使用大致相同的配置设置安装。 安装每台打印机会很耗时并且容易出错。 使用随 Microsoft .NET Framework一起公开的 System.Printing.IndexedProperties 命名空间和 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 安装包含新值的新打印机。

下面演示了一个示例。

                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 中的文档

打印概述