Compartir a través de


Cómo: Clonar una impresora

En la mayoría de las empresas, en algún punto se adquieren varias impresoras del mismo modelo. Normalmente, todas ellas se instalan configuraciones casi idénticas. Instalar cada impresora puede exigir mucho tiempo y dar lugar a errores. El espacio de nombres System.Printing.IndexedProperties y la clase InstallPrintQueue que se exponen con Microsoft .NET Framework permiten instalar al instante cualquier número de colas de impresión adicionales que se clonan a partir de una existente.

Ejemplo

En el ejemplo siguiente, se clona una segunda cola de impresión a partir de otra existente. La segunda se diferencia de la primera únicamente en su nombre, ubicación, puerto y estado compartido. Los pasos principales para hacerlo son los siguientes.

  1. Cree un objeto PrintQueue para la impresora existente que va a clonar.

  2. Cree un objeto PrintPropertyDictionary a partir de la propiedad PropertiesCollection del objeto PrintQueue. La propiedad Value de cada entrada de este diccionario es un objeto de uno de los tipos derivados de PrintProperty. Hay dos maneras de establecer el valor de una entrada en este diccionario.

    • Utilizar los métodos Remove y Add del diccionario para quitar la entrada y, a continuación, volver a agregarla con el valor deseado.

    • Utilizar el método SetProperty del diccionario.

    En el ejemplo siguiente se muestran ambas maneras.

  3. Cree un objeto PrintBooleanProperty y establezca su propiedad Name en "IsShared" y su propiedad Value en true.

  4. Utilice el objeto PrintBooleanProperty como valor de la entrada "IsShared" del objeto PrintPropertyDictionary.

  5. Cree un objeto PrintStringProperty y establezca su propiedad Name en "ShareName" y su propiedad Value en un valor de tipo String apropiado.

  6. Utilice el objeto PrintStringProperty como valor de la entrada "ShareName" del objeto PrintPropertyDictionary.

  7. Cree otro objeto PrintStringProperty y establezca su propiedad Name en "Location" y su propiedad Value en un valor de tipo String apropiado.

  8. Utilice el segundo objeto PrintStringProperty como valor de la entrada "Location" del objeto PrintPropertyDictionary.

  9. Cree una matriz de objetos String. Cada elemento es el nombre de un puerto del servidor.

  10. Utilice InstallPrintQueue para instalar la nueva impresora con los nuevos valores.

A continuación se muestra un ejemplo.

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

Vea también

Referencia

System.Printing.IndexedProperties

PrintPropertyDictionary

LocalPrintServer

PrintQueue

DictionaryEntry

Conceptos

Documentos en WPF

Información general sobre impresión