다음을 통해 공유


How to: Clone a Printer

Most businesses will, at some point, buy multiple printers of the same model. Typically, these are all installed with virtually identical configuration settings. Installing each printer can be time-consuming and error prone. The System.Printing.IndexedProperties namespace and the InstallPrintQueue class that are exposed with Microsoft .NET Framework version 3.0 makes it possible to instantly install any number of additional print queues that are cloned from an existing print queue.

Example

In the example below, a second print queue is cloned from an existing print queue. The second differs from the first only in its name, location, port, and shared status. The major steps for doing this are as follows.

  1. Create a PrintQueue object for the existing printer that is going to be cloned.

  2. Create a PrintPropertyDictionary from the PropertiesCollection of the PrintQueue. The Value property of each entry in this dictionary is an object of one of the types derived from PrintProperty. There are two ways to set the value of an entry in this dictionary.

    • Use the dictionary's Remove and Add methods to remove the entry and then re-add it with the desired value.

    • Use the dictionary's SetProperty method.

    The example below illustrates both ways.

  3. Create a PrintBooleanProperty object and set its Name to "IsShared" and its Value to true.

  4. Use the PrintBooleanProperty object to be the value of the PrintPropertyDictionary's "IsShared" entry.

  5. Create a PrintStringProperty object and set its Name to "ShareName" and its Value to an appropriate String.

  6. Use the PrintStringProperty object to be the value of the PrintPropertyDictionary's "ShareName" entry.

  7. Create another PrintStringProperty object and set its Name to "Location" and its Value to an appropriate String.

  8. Use the second PrintStringProperty object to be the value of the PrintPropertyDictionary's "Location" entry.

  9. Create an array of Strings. Each item is the name of a port on the server.

  10. Use InstallPrintQueue to install the new printer with the new values.

An example is below.

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

See Also

Reference

System.Printing.IndexedProperties
PrintPropertyDictionary
LocalPrintServer
PrintQueue
DictionaryEntry

Concepts

Documents in Windows Presentation Foundation
Printing Overview

Other Resources

Printing Samples