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


i have 2 network adapters in machine. e.g. adapter , adapter b. keeps connected

i want monitor connection status of adapter b. mean, connected or disconnected status.

i tried system.net.networkinformation.networkchange.networkavailabilitychanged

but doesn't work, since adapter connected.

maybe wmi helpful, don't have experience it, didn't find useful article.

could 1 show me

how subscribe wmi event of adapter connection status in c#

you can use win32_networkadapter wmi class , netconnectionstatus property __instancemodificationevent event.

the wql sentence so

select * __instancemodificationevent within 1 targetinstance isa 'win32_networkadapter' , targetinstance.name='network adapter name' 

try 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 * __instancemodificationevent within 1 "+                 "where targetinstance isa 'win32_networkadapter' , 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 exit");            eventwatcherasync eventwatcher = new eventwatcherasync();            console.read();         }     } } 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -