[C # source code] determines whether the network is connected.

Source: Internet
Author: User

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.