2009-05-20 14 views
7

Existe-t-il un moyen d'écrire du code pouvant «parler» à l'imprimante afin d'obtenir des informations de base sur son statut? Ce qui m'intéresse vraiment, c'est de savoir s'il n'y a plus de papier ou s'il y a un bourrage de papier - des choses de ce genre. Devrais-je utiliser la bibliothèque System.Management pour ce genre de choses? PS - Il serait également pratique de savoir comment avoir accès à toutes les imprimantes installées sur un PC en particulier. Comment allez-vous à ce sujet?Parler à une imprimante

+0

* met un chapeau de blague * mon père est un imprimeur et je lui parle tous les jours. –

+0

Stick à la programmation Olafur !! :) – Vidar

Répondre

9

L'obtention d'informations à partir d'imprimantes à l'aide de System.Management est relativement simple.

//Declare WMI Variables 
    ManagementObject MgmtObject; 
    ManagementObjectCollection MgmtCollection; 
    ManagementObjectSearcher MgmtSearcher; 

    //Perform the search for printers and return the listing as a collection 
    MgmtSearcher = new ManagementObjectSearcher("Select * from Win32_Printer"); 
    MgmtCollection = MgmtSearcher.Get(); 

    foreach (ManagementObject objWMI in MgmtCollection) 
    { 
     //Do whatever action you want with the Printer 
    } 

Regardez http://msdn.microsoft.com/en-us/library/aa394363.aspx pour les méthodes et les propriétés de Win32_Printer. Pour votre question:

//Test whether a Win32_Printer is out of paper or jammed 
int state = Int32.Parse(objWMI["PrinterState"]); 
if (state == 4) { 
    //Paper Jam 
} else if (state == 5) { 
    //Paper Out 
} 
Questions connexes