System.Management.ManagementScope oMs = new System.Management.ManagementScope(@"\\" + host + @"\root\cimv2");
            oMs.Connect();
            manEWatch = new ManagementEventWatcher(oMs, new EventQuery("SELECT * FROM    __InstanceCreationEvent WITHIN 0.1 WHERE TargetInstance ISA 'Win32_PrintJob'"));

            manEWatch.EventArrived += new EventArrivedEventHandler(mewPrintJobs_EventArrived);
            manEWatch.Start();

프린터에서 인쇄 이벤트가 발생될 경우 이벤트를 받아서 처리하는 코드..

 

        static void mewPrintJobs_EventArrived(object sender, EventArrivedEventArgs e)
        {
            foreach (PropertyData prop in e.NewEvent.Properties)
            {
                string val = prop.Value == null ? "null" : prop.Value.ToString();

            }

            ManagementBaseObject printJob = (ManagementBaseObject)e.NewEvent.Properties["TargetInstance"].Value;
            string v = "";
            foreach (PropertyData propp in printJob.Properties)
            {
                string name = propp.Name;
                string val = propp.Value == null ? "null" : propp.Value.ToString();
                val += "\n";
                v += name + ":" + val;

            }
            System.Windows.Forms.MessageBox.Show(v);
        }

 

이벤트에서 ManagementBaseObject 값을 출력해 보면 아래와 같다.

 

 

 

+ Recent posts