大多数企业将在某个时候购买同一型号的多台打印机。 通常,所有这些都安装有几乎相同的配置设置。 安装每个打印机可能非常耗时且容易出错。 使用 Microsoft .NET Framework 公开的 System.Printing.IndexedProperties 命名空间和 InstallPrintQueue 类可以立即安装从现有打印队列克隆的任意数量的附加打印队列。
示例:
在下面的示例中,从现有打印队列克隆第二个打印队列。 第二个与第一个不同,仅在名称、位置、端口和共享状态上有差异。 执行此操作的主要步骤如下。
为要克隆的现有打印机创建 PrintQueue 对象。
从 PrintPropertyDictionary 的 PropertiesCollection 中创建 PrintQueue。 此字典中每个条目的 Value 属性是派生自 PrintProperty的类型之一的对象。 可通过两种方式在此字典中设置条目的值。
使用字典的 Remove 和 Add 方法来删除条目,然后用所需的值重新添加该条目。
使用字典的 SetProperty 方法。
下面的示例演示了这两种方式。
创建 PrintBooleanProperty 对象并将其 Name 设置为“IsShared”,并将其 Value 设置为
true
。使用 PrintBooleanProperty 对象作为 PrintPropertyDictionary'IsShared' 条目的值。
创建 PrintStringProperty 对象并将其 Name 设置为“ShareName”,并将其 Value 设置为适当的 String。
使用 PrintStringProperty 对象作为 PrintPropertyDictionary的“ShareName”条目的值。
创建另一个 PrintStringProperty 对象,并将其 Name 设置为“位置”,并将其 Value 设置为适当的 String。
使用第二个 PrintStringProperty 对象作为 PrintPropertyDictionary“位置”条目的值。
创建一个 String 数组。 每个项都是服务器上的端口的名称。
使用 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()