Share via


Procedura: duplicare una stampante

Molte aziende desiderano, in un determinato momento, comprare più stampanti dello stesso modello. In genere, queste vengono installate con impostazioni di configurazione praticamente identiche. L'installazione delle stampanti una ad una può comportare tempi lunghi e un rischio di errori. Lo spazio dei nomi System.Printing.IndexedProperties e la classe InstallPrintQueue esposti con Microsoft .NET Framework consentono l'installazione istantanea di un numero di code di stampa indeterminato, che vengono duplicate in base a una coda di stampa esistente.

Esempio

Nell'esempio riportato di seguito, viene duplicata una seconda coda di stampa in base a una coda di stampa esistente. La seconda differisce dalla prima solo in relazione al nome, alla posizione, alla porta e allo stato condiviso. I passaggi principali di questa procedura sono indicati di seguito.

  1. Creare un oggetto PrintQueue per la stampante esistente che sarà duplicata.

  2. Creare un oggetto PrintPropertyDictionary dall'oggetto PropertiesCollection di PrintQueue. La proprietà Value di ciascun elemento di questo dizionario è un oggetto di uno dei tipi derivati da PrintProperty. È possibile impostare il valore di un elemento di questo dizionario in due modi.

    • Utilizzare i metodi Remove e Add del dizionario per rimuovere l'elemento e quindi aggiungerlo nuovamente con il valore desiderato.

    • Utilizzare il metodo SetProperty del dizionario.

    Entrambi i metodi vengono illustrati nell'esempio riportato di seguito.

  3. Creare un oggetto PrintBooleanProperty e impostare Name su "IsShared" e Value su true.

  4. Utilizzare l'oggetto PrintBooleanProperty come valore dell'elemento "IsShared" di PrintPropertyDictionary.

  5. Creare un oggetto PrintStringProperty e impostare Name su "ShareName" e Value su un valore di String appropriato.

  6. Utilizzare l'oggetto PrintStringProperty come valore dell'elemento "ShareName" di PrintPropertyDictionary.

  7. Creare un oggetto PrintStringProperty e impostare Name su "Location" e Value su un valore di String appropriato.

  8. Utilizzare il secondo oggetto PrintStringProperty come valore dell'elemento "Location" di PrintPropertyDictionary.

  9. Creare una matrice di String. Ogni elemento è il nome di una porta del server.

  10. Utilizzare InstallPrintQueue per installare la nuova stampante con i nuovi valori.

Di seguito viene riportato un esempio.

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

Vedere anche

Riferimenti

System.Printing.IndexedProperties

PrintPropertyDictionary

LocalPrintServer

PrintQueue

DictionaryEntry

Concetti

Documenti in WPF

Cenni preliminari sulla stampa