C # How to check the network connection status

Source: Internet
Author: User

In some programs, you may want to check whether the network is connected and do not want to sell too much system resources. The following method is to directly call the system API for detection.

1. method definition
[DllImport ("wininet. dll")]
Private extern static bool InternetGetConnectedState (out int connectionDescription, int reservedValue );

2. Method description
Parameters:
ConnectionDescription: Connection description
ReservedValue: Reserved value
Return Value:
True: On Line
False: Off Line

3. Call Method
A. You must reference System. Runtime. InteropServices in your code. Otherwise, compilation errors may occur.
B. Define a variable int I = 0;
C. Call bool state = InternetGetConnectedState (out I, 0 );

4. complete code:

1 using System. Runtime. InteropServices;
2 namespace internet
3 {
4 public class Class1
5 {
6 [DllImport ("wininet. dll")]
7 private extern static bool InternetGetConnectedState (out int connectionDescription, int reservedValue );
8 public Class1 (){}
9 private bool IsConnected ()
10 {
11 int I = 0;
12 bool state = InternetGetConnectedState (out I, 0 );
13 return state;
14}
15}
16}

Related Article

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.