Visualizzazione dei risultati da 1 a 8 su 8
  1. #1

    [C#] Clonare una stampante

    Ciao a tutti,
    vorrei imlementare una funzione trovata su msdn la quale con un esempio permette di "Clonare una stampante" (documentazione http://msdn.microsoft.com/en-us/library/aa970846.aspx ).
    il codice c# è questo, non è nulla di complicato e potete provarlo, il fatto è che da un eccezione "Impossibile eseguire il cast di oggetti di tipo 'System.Printing.PrintQueueAttributes' sul tipo 'System.Int32'."...pare sia un problema noto visto che l'hanno anche nel "Community Content" ma proprio nn riesco a capire come risolverlo:

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

  2. #2
    Utente di HTML.it L'avatar di albgen
    Registrato dal
    Jun 2005
    Messaggi
    3,249
    il codice che hai postato compila!
    I got the remedy

  3. #3
    certo...a compilare compila...ma prova a mandare in run o "Avvia debug"...restituirà quell errore!

  4. #4
    Utente di HTML.it L'avatar di albgen
    Registrato dal
    Jun 2005
    Messaggi
    3,249
    Originariamente inviato da ispooky83
    certo...a compilare compila...ma prova a mandare in run o "Avvia debug"...restituirà quell errore!
    si...va in errore, perchè probabilmente manca il driver, o no
    I got the remedy

  5. #5
    Originariamente inviato da albgen
    si...va in errore, perchè probabilmente manca il driver, o no
    Non credo che il problema sia che manca il driver...visto che io ho scritto tutto correttamente...inoltre a fine pagina del link http://msdn.microsoft.com/en-us/library/aa970846.aspx c'è un utente che evidenzia l'"InvalidCastException"...quindi credo sia un problema noto.
    Inoltre nella rete ho trovato un utente che ha notato l'errore e l'ha risolto...ma nn riesco a capire in che modo...posto il link
    http://groups.google.com/group/micro...e85bee66bee3cd

  6. #6
    Utente di HTML.it L'avatar di albgen
    Registrato dal
    Jun 2005
    Messaggi
    3,249
    In quel link dice che ha trovato delle chiavi che hanno valori non inizializzati, ma non dice quali. E cmq, anche se lo dicesse sarebbe poco utile perchè dipende dalla stampante.
    Io ho provato cosi' ed ha funzionato. tieni presente che il codice che hai postato crea una copia della stampante predefinita...

    PrintQueue clonedPrinter = myLocalPrintServer.InstallPrintQueue("My clone of " + sourcePrintQueue.Name, sourcePrintQueue.Name, port, "WinPrint", PrintQueueAttributes.None);
    I got the remedy

  7. #7
    Originariamente inviato da albgen
    In quel link dice che ha trovato delle chiavi che hanno valori non inizializzati, ma non dice quali. E cmq, anche se lo dicesse sarebbe poco utile perchè dipende dalla stampante.
    Io ho provato cosi' ed ha funzionato. tieni presente che il codice che hai postato crea una copia della stampante predefinita...

    PrintQueue clonedPrinter = myLocalPrintServer.InstallPrintQueue("My clone of " + sourcePrintQueue.Name, sourcePrintQueue.Name, port, "WinPrint", PrintQueueAttributes.None);
    In realtà è proprio il mio obbiettivo...clonare una stampante ma cambiandogli le impostazioni, come ad esempio cambiare il formato carta o il fatto che stampi in duplex.
    Ora passandogli "PrintQueueAttributes.None" non gli passo alcuna funzione...come posso fare per camibiare queste impostazioni??

  8. #8
    Utente di HTML.it L'avatar di albgen
    Registrato dal
    Jun 2005
    Messaggi
    3,249
    fai una cosa del genere:

    foreach (DictionaryEntry dictionaryEntry in sourcePrintQueue.PropertiesCollection)
    {
    PrintProperty property = (PrintProperty)dictionaryEntry.Value;

    property.Value = //quello che vuoi...

    }

    ecco come si può impostare una proprietà
    http://msdn.microsoft.com/en-us/libr...intticket.aspx
    I got the remedy

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.