Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using system. net. networkinformation;
Using system. runtime. interopservices;
Namespace Network
{
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
Networkchange. networkavailabilitychanged + = new networkavailabilitychangedeventhandler (networkchange_networkavailabilitychanged );
Networkchange. networkaddresschanged + = new networkaddresschangedeventhandler (networkchange_networkaddresschanged );
}
Public void networkchange_networkaddresschanged (Object sender, eventargs E)
{
Networkinterface [] adapters = networkinterface. getallnetworkinterfaces ();
Foreach (networkinterface N in adapters)
{
If (N. networkinterfacetype! = Networkinterfacetype. loopback)
Console. writeline ("changed {0} is {1}", N. Name, N. operationalstatus );
}
Console. writeline ("netstate:" + netstate ());
}
Public void networkchange_networkavailabilitychanged (Object sender, networkavailabilityeventargs E)
{
Console. writeline ("isavailable:" + E. isavailable );
Console. writeline ("netstate:" + netstate ());
}
[Dllimport ("wininet. dll")]
Public static extern bool internetgetconnectedstate (out long lpdwflags, long dwreserved );
Public static bool netstate ()
{
// String url = http://www.baidu.com /;
// If (URL. startswith ("http: //") url = URL. Remove (0, "http: //". Length /);
// Try
//{
// System. net. iphostentry iphost = system. net. DNS. gethostentry (URL); // system. net. DNS. Resolve (URL );
// Return true;
//}
// Catch // (system. net. sockets. socketexception SE)
//{
/// System. Diagnostics. Trace. Write (SE. Message );
// Return false;
//}
Try
{
Long lfag;
If (internetgetconnectedstate (Out lfag, 0 ))
{
Return true;
}
Return false;
}
Catch
{
Return false;
}
}
}
}