Como clonar uma impressora

A maioria das empresas comprará várias impressoras do mesmo modelo em algum momento. Normalmente, elas são instaladas com configurações praticamente idênticas. Instalar cada impressora pode ser um processo demorado e sujeito a erros. O System.Printing.IndexedProperties namespace e a classe expostos com o Microsoft .NET Framework possibilitam a InstallPrintQueue instalação instantânea de qualquer número de filas de impressão adicionais que são clonadas de uma fila de impressão existente.

Exemplo

No exemplo abaixo, uma segunda fila de impressão é clonada de uma fila de impressão existente. A segunda difere da primeira somente no nome, localização, porta e status compartilhado. As principais etapas para fazer isso são as seguintes.

  1. Crie um PrintQueue objeto para a impressora existente que será clonada.

  2. Crie um PrintPropertyDictionary a partir do PropertiesCollectionPrintQueue. A Value propriedade de cada entrada neste dicionário é um objeto de um dos tipos derivados de PrintProperty. Há duas maneiras de definir o valor de uma entrada neste dicionário.

    • Use os métodos Remove e do dicionário para remover a entrada e Add adicioná-la novamente com o valor desejado.

    • Use o método do SetProperty dicionário.

    O exemplo abaixo ilustra ambas as direções.

  3. Crie um PrintBooleanProperty objeto e defina-o Name como "IsShared" e seu Value como true.

  4. Use o PrintBooleanProperty objeto para ser o PrintPropertyDictionaryvalor da entrada "IsShared" do .

  5. Crie um objeto e defina-o Name como "ShareName" e seu Value como um StringPrintStringProperty arquivo .

  6. Use o objeto para ser o PrintStringProperty valor da PrintPropertyDictionaryentrada "ShareName" do .

  7. Crie outro PrintStringProperty objeto e defina-o Name como "Location" e seu Value como um arquivo String.

  8. Use o segundo PrintStringProperty objeto para ser o PrintPropertyDictionaryvalor da entrada "Location" do .

  9. Crie uma matriz de Strings. Cada item é o nome de uma porta no servidor.

  10. Use InstallPrintQueue para instalar a nova impressora com os novos valores.

Veja um exemplo abaixo.

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

Confira também