Condividi tramite


Procedura: Clonare una stampante

La maggior parte delle aziende, a un certo punto, acquista più stampanti dello stesso modello. In genere, questi sono tutti installati con impostazioni di configurazione virtualmente identiche. L'installazione di ogni stampante può richiedere molto tempo e causare errori. Lo spazio dei nomi System.Printing.IndexedProperties e la classe InstallPrintQueue esposti con Microsoft .NET Framework consente di installare immediatamente qualsiasi numero di code di stampa aggiuntive clonate da una coda di stampa esistente.

Esempio

Nell'esempio seguente viene clonata una seconda coda di stampa da una coda di stampa esistente. Il secondo differisce dal primo solo nel nome, nella localizzazione, nella porta e nello stato di condivisione. I passaggi principali per eseguire questa operazione sono i seguenti.

  1. Creare un oggetto PrintQueue per la stampante esistente che verrà clonata.

  2. Creare un PrintPropertyDictionary dal PropertiesCollection del PrintQueue. La proprietà Value di ogni voce di questo dizionario è un oggetto di uno dei tipi derivati da PrintProperty. Esistono due modi per impostare il valore di una voce in questo dizionario.

    • Usare i metodi Remove e Add del dizionario per rimuovere la voce e quindi aggiungerla nuovamente con il valore desiderato.

    • Usare il metodo SetProperty del dizionario.

    L'esempio seguente illustra entrambi i modi.

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

  4. Utilizzare l'oggetto PrintBooleanProperty come valore della voce "IsShared" del PrintPropertyDictionary.

  5. Creare un oggetto PrintStringProperty e impostarne il Name su "ShareName" e il Value su un Stringappropriato.

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

  7. Creare un altro oggetto PrintStringProperty e impostarne il Name su "Posizione" e il relativo Value su un Stringappropriato.

  8. Utilizzare il secondo oggetto PrintStringProperty come valore della voce "Location" del PrintPropertyDictionary.

  9. Creare una matrice di Strings. Ogni elemento è il nome di una porta nel server.

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

Di seguito è riportato un esempio.

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

Vedere anche