【WPF.NET开发】克隆打印机

发布时间:2024年01月23日

本文内容

大多数企业有时会购买多台同一型号的打印机。 通常,这些打印机都安装了几乎相同的配置设置。 安装每台打印机既费时又容易出错。 使用 Microsoft .NET Framework 公开的?System.Printing.IndexedProperties?命名空间和?InstallPrintQueue?类可以立即安装从现有打印队列克隆的任意数量的附加打印队列。

示例

在下面的示例中,从现有打印队列克隆第二个打印队列。 第二个队列与第一个队列的不同之处仅在于名称、位置、端口和共享状态。 执行此操作的主要步骤如下。

  1. 为将要克隆的现有打印机创建?PrintQueue?对象。

  2. 从?PrintQueue?的?PropertiesCollection?中创建?PrintPropertyDictionary。 此字典中每个条目的?Value?属性是从?PrintProperty?派生的类型之一的对象。 可通过两种方式在此字典中设置条目的值。

    • 使用字典的 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?对象并将其?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();

?

文章来源:https://blog.csdn.net/m0_51887793/article/details/135792176
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。