2009-05-13 6 views

Répondre

2

Voici un exemple d'utilisation de l'espace de noms System.Management pour accéder WMI et imprimer une page de test à une imprimante. Cela dépend de l'imprimante étant connectée à l'ordinateur, je peux fournir le code pour connecter une imprimante réseau via System.Management si vous le souhaitez aussi. Ce code devrait fonctionner pour toute version du Framework .Net

using System; 
using System.Management; 

public class PrintTestPageUsingWMI 
{ 
    private String _name; 
    private ManagementObject _printer = null; 

    public PrintTestPageUsingWMI(String printerName) 
    { 
     this._name = printerName; 

     //Find the Win32_Printer which is a Network Printer of this name 

     //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) 
     { 
      if (objWMI.Item("sharename").ToString().Equals(this._name)) 
      { 
       this._printer = objWMI; 
      } 
     } 

     if (this._printer == null) 
     { 
      throw new Exception("Selected Printer is not connected to this Computer"); 
     }   
    } 

    public void PrintTestPage() 
    { 
     this.InvokeWMIMethod("PrintTestPage"); 
    } 

    /// <summary> 
    /// Helper Method which Invokes WMI Methods on this Printer 
    /// </summary> 
    /// <param name="method">The name of the WMI Method to Invoke</param> 
    /// <remarks></remarks> 
    private void InvokeWMIMethod(String method) { 
     if (this._printer == null) 
     { 
      throw new Exception("Can't Print a Test Page on a Printer which is not connected to the Computer"); 
     } 

     Object[] objTemp = new Object[0] { null }; 
     ManagementObject objWMI; 

     //Invoke the WMI Method 
     this._printer.InvokeMethod(method, objTemp); 
    } 
} 

Sinon, vous pouvez regarder l'espace de noms System.Printing qui est pris en charge .Net 3.0 et versions ultérieures

Questions connexes