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}