-1

Is there a way to detect a configuration changes in printers using C#?

I have thread in c# which usually sleeps and I want it to be notified when there is any change in configuration (like someone adds/removes/updates the printer or someone changes the default printer). Once it is notified, it will display simple message.

Is this possible using C#.NET or Using WMI? I have already gone through the solution available but none of them seems to be suitable for the requirement I have.

atp9
  • 890
  • 1
  • 11
  • 23
  • You'll probably get better answers if you write what are the solutions you already tried and why they aren't suitable for your requirements – Nir Jul 28 '15 at 12:52
  • 1
    Like I said, **I am looking for a way to do so**. I have reviewed the help available in various forums and as none of them were helping to solve my purpose, I have not implemented any. But for the information, the most closer I could reach to is [Here](http://stackoverflow.com/questions/20529075/how-do-i-tell-when-the-default-printer-was-changed). – atp9 Jul 28 '15 at 12:58
  • From the question you linked to it looks like FindFirstPrinterChangeNotification with PRINTER_CHANGE_PRINTER will detect adding/removing printers and the answer to that question tells you how to detect default printer changes – Nir Jul 28 '15 at 13:06
  • Yeah but that is something very specific. I am still searching on other forums and blogs. – atp9 Jul 28 '15 at 13:46

1 Answers1

1

You can monitor the printer configuration changes using the __InstanceModificationEvent event and the Win32_Printer WMI class

Try this sample.

using System;
using System.Collections.Generic;
using System.Management;
using System.Text;


namespace GetWMI_Info
{
    public class EventWatcherAsync
    {
        private void WmiEventHandler(object sender, EventArrivedEventArgs e)
        {
            Console.WriteLine("TargetInstance.Name :       " + ((ManagementBaseObject)e.NewEvent.Properties["TargetInstance"].Value)["Name"]);

        }

        public EventWatcherAsync()
        {
            try
            {
                string ComputerName = "localhost";
                string WmiQuery;
                ManagementEventWatcher Watcher;
                ManagementScope Scope;


                if (!ComputerName.Equals("localhost", StringComparison.OrdinalIgnoreCase))
                {
                    ConnectionOptions Conn = new ConnectionOptions();
                    Conn.Username = "";
                    Conn.Password = "";
                    Conn.Authority = "ntlmdomain:DOMAIN";
                    Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", ComputerName), Conn);
                }
                else
                    Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", ComputerName), null);
                Scope.Connect();

                WmiQuery = "Select * From __InstanceModificationEvent Within 1 " +
                "Where TargetInstance ISA 'Win32_Printer' ";

                Watcher = new ManagementEventWatcher(Scope, new EventQuery(WmiQuery));
                Watcher.EventArrived += new EventArrivedEventHandler(this.WmiEventHandler);
                Watcher.Start();
                Console.Read();
                Watcher.Stop();
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception {0} Trace {1}", e.Message, e.StackTrace);
            }

        }

        public static void Main(string[] args)
        {
            Console.WriteLine("Listening {0}", "__InstanceModificationEvent");
            Console.WriteLine("Press Enter to exit");
            EventWatcherAsync eventWatcher = new EventWatcherAsync();
            Console.Read();
        }
    }
}
RRUZ
  • 134,889
  • 20
  • 356
  • 483