Procedura: duplicare 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 System.Printing.IndexedProperties spazio dei nomi e la InstallPrintQueue classe esposti con Microsoft .NET Framework consentono 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 posizione, nella porta e nello stato condiviso. I passaggi principali per eseguire questa operazione sono i seguenti.

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

  2. Creare un PrintPropertyDictionary oggetto dall'oggetto PropertiesCollection dell'oggetto PrintQueue. La Value proprietà 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 remove del dizionario per rimuovere la voce e quindi aggiungerla nuovamente con il valore desiderato.

    • Usare il metodo del SetProperty dizionario.

    L'esempio seguente illustra entrambi i modi.

  3. Creare un PrintBooleanProperty oggetto e impostarlo Name su "IsShared" e su Valuetrue.

  4. Utilizzare l'oggetto PrintBooleanProperty come valore della PrintPropertyDictionaryvoce "IsShared".

  5. Creare un PrintStringProperty oggetto e impostarlo Name su "ShareName" e su Value un oggetto appropriato String.

  6. Utilizzare l'oggetto PrintStringProperty come valore della PrintPropertyDictionaryvoce "ShareName".

  7. Creare un altro PrintStringProperty oggetto e impostarne il valore Name su "Location" e su Value un oggetto appropriato String.

  8. Usare il secondo PrintStringProperty oggetto come valore della PrintPropertyDictionaryvoce "Location" di .

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

Vedi anche