using System;
using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Management;namespace Record
{ public partial class Form1 : Form { public Form1() { InitializeComponent(); }private void button1_Click(object sender, EventArgs e)
{ ManagementClass newClass = new ManagementClass();//Set up an event watcher and a handler for the event
ManagementEventWatcher networkAdapterArrivalWatcher = new ManagementEventWatcher("", "SELECT * FROM MSNdis_NotifyAdapterArrival ");ManagementEventWatcher networkAdapterRemovalWatcher =
new ManagementEventWatcher("", "SELECT * FROM MSNdis_NotifyAdapterRemoval ");MyHandler handler = new MyHandler();
networkAdapterArrivalWatcher.EventArrived += new EventArrivedEventHandler(handler.Arrived); networkAdapterRemovalWatcher.EventArrived += new EventArrivedEventHandler(handler.Removed); //Start watching for events networkAdapterArrivalWatcher.Start(); networkAdapterRemovalWatcher.Start(); //while (true) // System.Threading.Thread.Sleep(200000); //IsNetworkAvailable; } } public class MyHandler { public void Arrived(object sender, EventArrivedEventArgs e) { Console.WriteLine("Network connected"); }public void Removed(object sender, EventArrivedEventArgs e)
{ Console.WriteLine("Network disconnected"); } } }本文转自94cool博客园博客,原文链接:http://www.cnblogs.com/94cool/archive/2009/08/14/1545775.html,如需转载请自行联系原作者