Method 1:
Using System; Using System. Collections. Generic; Using System. componentmodel; Using System. Data; Using System. drawing; Using System. LINQ; Using System. text; Using System. Windows. forms; Using System. net. networkinformation; Namespace Windowsformsapplication1 { Public Partial Class Form1: FORM { Public Form1 () {initializecomponent ();} Private Void Form1_load ( Object Sender, eventargs e) {ping = New Ping (); pingreply re = A. Send ( " 202.96.134.134 " ); // Ping Return Value If (Re. Status = ipstatus. Success) // If Ping is successful {Label1.text = " Internet connection successful .. " ; Label1.forecolor = Color. Green ;} Else {Label1.text = " An error occurred while connecting to the Internet. " ; Label1.forecolor = Color. Red ;}}}}
Method 2:
Using System; Using System. runtime. interopservices; Namespace Network_status { Class Program {[dllimport ( " Wininet. dll " , Entrypoint = " Internetgetconnectedstate " )] Public Extern Static Bool Internetgetconnectedstate ( Out Int Constate, Int Reder ); // Parameter description constate connection description, reder reserved value Public Static Bool Isconnectedtointernet (){ Int Desc = 0 ; Return Internetgetconnectedstate ( Out DESC, 0 );} Public Static Void Main ( String [] ARGs ){ While ( True ){ If (Isconnectedtointernet () {console. writeline ( " Connected to the internet! " ); // Inform me of code .... } Else {Console. writeline ( " Not connected to the internet! " );} System. Threading. thread. Sleep ( 5000 );}}}}
OriginalArticle, Reprint please indicate the source: http://www.cnblogs.com/hongfei