Compartilhar via


Como clonar uma impressora

A maioria das empresas comprará, em algum momento, várias impressoras do mesmo modelo. Normalmente, todas elas são instaladas com configurações praticamente idênticas. A instalação de cada impressora pode ser demorada e propensa a erros. O namespace System.Printing.IndexedProperties e a classe InstallPrintQueue expostos pelo Microsoft .NET Framework possibilitam instalar instantaneamente 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. O segundo é diferente do primeiro apenas em seu nome, local, 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 PropertiesCollection do PrintQueue. 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 Remover e Add do dicionário para remover a entrada e, em seguida, adicioná-la novamente com o valor desejado.

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

    O exemplo a seguir ilustra as duas maneiras.

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

  4. Use o PrintBooleanProperty objeto como valor da entrada "IsShared" de PrintPropertyDictionary.

  5. Crie um objeto PrintStringProperty e defina seu Name para "ShareName" e seu Value para um String apropriado.

  6. Use o objeto PrintStringProperty para ser o valor do campo "ShareName" de PrintPropertyDictionary.

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

  8. Use o segundo objeto PrintStringProperty como valor da entrada "Localização" de PrintPropertyDictionary.

  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.

Um exemplo está 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()

Consulte também