Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to subscribe a specific network adapter connection event in .NET

I have two network adapters in my machine. E.g. adapter A and adapter B. The A keeps connected

I want to monitor the connection status of adapter B. I mean, connected or disconnected status.

I tried the System.Net.NetworkInformation.NetworkChange.NetworkAvailabilityChanged

But it doesn't work, since the adapter A connected.

Maybe WMI is helpful, but I don't have any experience about it, also didn't find much useful article.

Could any one help to show me

how to subscribe WMI event of the adapter A connection status in C#

like image 536
aguai Avatar asked May 03 '13 09:05

aguai


1 Answers

You can use the Win32_NetworkAdapter WMI class and the NetConnectionStatus property with the __InstanceModificationEvent event.

The WQL sentence will look like so

Select * From __InstanceModificationEvent Within 1 Where TargetInstance ISA 'Win32_NetworkAdapter' AND TargetInstance.Name='Network adapter name'

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"]);
            //2 (0x2) Connected
            //7 (0x7) Media disconnected
            Console.WriteLine("TargetInstance.NetConnectionStatus :    " + ((ManagementBaseObject)e.NewEvent.Properties["TargetInstance"].Value)["NetConnectionStatus"]);

        }

        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_NetworkAdapter' AND TargetInstance.Name='Tarjeta Mini de media altura WLAN Wireless-N DW1501' ";

                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();
        }
    }
}
like image 195
RRUZ Avatar answered Sep 22 '22 12:09

RRUZ