Network Change Events in .NET 1.1

i currently prepare the slides for my WMI talk at DevWeek. in case you wondered, this is the async version of the NetworkChangeWatcher:

static void Main(string[] args)
   WqlEventQuery query =
    new WqlEventQuery(“__InstanceModificationEvent”,
    new TimeSpan(0,0,5),
    “TargetInstance isa “Win32_NetworkAdapterConfiguration””); 

   ManagementEventWatcher eventWatcher = new ManagementEventWatcher(query);
   eventWatcher.EventArrived += new EventArrivedEventHandler(Delegate_EventArrived);
   // start listening for events

private static void Delegate_EventArrived(object sender, EventArrivedEventArgs e)
   uint index = System.Convert.ToUInt32(((ManagementBaseObject)e.NewEvent[“TargetInstance”])[“Index”]);

   Console.WriteLine(“NIC #{0} has changed its state”, index); 


i had a look at the new System.Net.NetworkInformation namespace in whidbey. this looks promising!

if you are writing apps that have to be aware of network and connectivity changes you can accomplish the same as Whidbey’s NetworkChange events with WMI Events.

You first have to create a WMI EventQuery. In our case this means: “tell me when a instance of the win32_NetworkAdaptedConfiguration class gets modified. I will ask you every 5 seconds.” With the ManagementWatcher you create the transient event subscription.

The WaitForNextEvent() call is blocking, so you should run the subscription loop on a different thread or use the asynchronous WMI subscription service.

WqlEventQuery query =
    new WqlEventQuery(“__InstanceModificationEvent”,
    new TimeSpan(0,0,5),
    “TargetInstance isa “Win32_NetworkAdapterConfiguration””);

   ManagementEventWatcher watcher = new ManagementEventWatcher(query);

   // you could loop here 
   ManagementBaseObject evt = watcher.WaitForNextEvent();

   // retrieve the index of the network card that changed   
   uint index = System.Convert.ToUInt32(((ManagementBaseObject)evt[“TargetInstance”])[“Index”]);

   // create a WMI wrapper class
   ROOT.CIMV2.NetworkAdapterConfiguration nic = new NetWatcher.ROOT.CIMV2.NetworkAdapterConfiguration(index);
   Console.WriteLine(“{0} : {1} / {2}”, nic.Index, nic.IPAddress[0], nic.IPSubnet[0]);



btw – if you are doing WMI programming with Visual Studio you should check out the WMI Extensions.

did you notice the wrapper class in the above sample? it was generated by the WMI Extensions. To get this class:

  • Install the WMI Extensions
  • Open Visual Studio
  • New Project
  • Go to Server Explorer / Management Classes
  • Right Click -> Add Class
  • Drill down to rootCIMV2 : Network Adapter Settings
  • Click Add and OK
  • Right click on the new node -> Generate Managed Class

You can also call all WMI Methods trough the Server Explorer and test WMI Event Queries. useful stuff.


This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s