Udostępnij za pośrednictwem


Instrukcje: klonowanie drukarki

Większość firm kupi w pewnym momencie wiele drukarek tego samego modelu. Zazwyczaj są one instalowane z praktycznie identycznymi ustawieniami konfiguracji. Instalowanie każdej drukarki może być czasochłonne i podatne na błędy. Przestrzeń nazw System.Printing.IndexedProperties i klasa InstallPrintQueue uwidoczniona w programie Microsoft .NET Framework umożliwia natychmiastowe zainstalowanie dowolnej liczby dodatkowych kolejek wydruku sklonowanych z istniejącej kolejki wydruku.

Przykład

W poniższym przykładzie druga kolejka drukowania jest tworzona jako kopia istniejącej kolejki drukowania. Drugi różni się od pierwszego tylko w nazwie, lokalizacji, porcie i statusie udostępnienia. Główne kroki w tym celu są następujące.

  1. Utwórz obiekt PrintQueue dla istniejącej drukarki, która ma zostać sklonowana.

  2. Utwórz PrintPropertyDictionary na podstawie PropertiesCollection z PrintQueue. Właściwość Value każdego wpisu w tym słowniku jest obiektem jednego z typów pochodzących z PrintProperty. Istnieją dwa sposoby ustawiania wartości wpisu w tym słowniku.

    • Użyj metody Remove i Add słownika, aby usunąć wpis, a następnie ponownie dodać go z żądaną wartością.

    • Użyj metody słownika SetProperty.

    W poniższym przykładzie przedstawiono oba sposoby.

  3. Utwórz obiekt PrintBooleanProperty i ustaw jego Name na "IsShared" i jego Value na true.

  4. Użyj obiektu PrintBooleanProperty jako wartość we wpisie "IsShared" w PrintPropertyDictionary.

  5. Utwórz obiekt PrintStringProperty i ustaw jego Name na wartość "ShareName" oraz przypisz jego Value do odpowiedniego String.

  6. Użyj obiektu PrintStringProperty jako wartości dla pozycji „ShareName” w PrintPropertyDictionary.

  7. Utwórz inny obiekt PrintStringProperty i ustaw jego Name na "Lokalizacja" i jego Value na odpowiedni String.

  8. Użyj drugiego obiektu PrintStringProperty, aby być wartością wpisu PrintPropertyDictionary"Lokalizacja".

  9. Utwórz tablicę Strings. Każdy element jest nazwą portu na serwerze.

  10. Użyj InstallPrintQueue, aby zainstalować nową drukarkę z nowymi wartościami.

Poniżej przedstawiono przykład.

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

Zobacz także