Network Connection Detection C#

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi, <br/>I have been trying to have my application looking for a connection<br/><br/>I have this code in my public MainForm() after InitializeComponent();<br/><br/><br/><br/>
<pre lang="x-c# ManagementClass newClass = new ManagementClass();
ManagementEventWatcher networkAdapterArrivalWatcher =
new ManagementEventWatcher("\root\wmi",
"SELECT * FROM MSNdis_NotifyAdapterArrival ");
ManagementEventWatcher networkAdapterRemovalWatcher =
new ManagementEventWatcher("\root\wmi",
"SELECT * FROM MSNdis_NotifyAdapterRemoval ");
MyHandler handler = new MyHandler();
networkAdapterArrivalWatcher.EventArrived += new EventArrivedEventHandler(handler.Arrived);
networkAdapterRemovalWatcher.EventArrived += new EventArrivedEventHandler(handler.Removed);
networkAdapterArrivalWatcher.Start();
networkAdapterRemovalWatcher.Start();
while (true)
System.Threading.Thread.Sleep(200000);[/code]
and I have this code in public partial class MainForm : Form { ... }<br/><br/><br/>
<pre> public class MyHandler
{
public void Arrived(object sender, EventArrivedEventArgs e)
{
Console.WriteLine("Network connected");
MessageBox.Show("Test");
}

public void Removed(object sender, EventArrivedEventArgs e)
{
Console.WriteLine("Network disconnected");
MessageBox.Show("Test");
}

}[/code]
but when I disconnect my cable or connect it, nothing happens. <br/>Anyone has any input ? Am I loading the code at the wrong spot ? <br/><br/>Thanks for help

View the full article
 
Back
Top