Ask a questionAsk a question
 

Questionusing WMI to monitor printer usage

  • Tuesday, November 03, 2009 5:46 AMedmorekachere Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    I have the code below.I want to make sure that everytime a print job is finished the total number of pages printed and the owner of the document is stored in a databases.How do i make sure this is done .i have creted an event watcher that shows every print job on a printer.how can i save the total number of pages printed after the event has been detected and the print job is finished



    using System;
    using System.ComponentModel;
    using System.Runtime.InteropServices;
    using System.Management;
    class WMIEvent {
    public static void Main() {
    WMIEvent we = new WMIEvent();
    ManagementEventWatcher w= null;
    WqlEventQuery q;
    ManagementOperationObserver observer = new ManagementOperationObserver();
    // Bind to local machine
    ManagementScope scope = new ManagementScope("root\\CIMV2");
    // scope.Options.EnablePrivileges = true; //sets required privilege
    try {
    q = new WqlEventQuery();
    q.EventClassName = "__InstanceOperationEvent";
    q.Condition = @"TargetInstance ISA 'Win32_PrintJob'";
    q.WithinInterval = new TimeSpan(0,0,10);
    w = new ManagementEventWatcher(scope, q);
    w.EventArrived += new EventArrivedEventHandler(we.PrJobEventArrived);
    w.Start();
    Console.ReadLine(); // Block this thread - Press enter to terminate
    w.Stop();
    }
    catch(Exception e) {
    Console.WriteLine(e);
    }
    }

    public void PrJobEventArrived(object sender, EventArrivedEventArgs e) {
    //Get the Event object and display all properies
    foreach(PropertyData pd in e.NewEvent.Properties) {
    Console.WriteLine("\n======================================");
    Console.WriteLine("{0},{1},{2}, {3}",pd.Name, pd.Type, pd.Value,
    pd.Origin);
    ManagementBaseObject mbo = null;
    if(( mbo = pd.Value as ManagementBaseObject) != null) {
    Console.WriteLine("--------------Properties------------------");
    foreach(PropertyData prop in mbo.Properties)
    Console.WriteLine("{0} - {1}", prop.Name, prop.Value);
    }
    }
    }
    }