How to detect the connection status of the network (C #)

xiaoxiao2021-03-06  46

In some programs, you may want to detect if the network is connected, but do not want to consume too much system resources, the following method is to directly call the system API to detect.

Method definition

[DLLIMPORT ("Wininet.dll")]]]]]

Private Extern Static Bool InternetGetConnectedState (Out Int ConnectionDescription, Int ReservedValue);

2. Method Description

parameter:

ConnectionDescription: Connection Description

ReservedValue: Reserved value

return value:

True: on line

False: off line

3. Calling method

a. You must reference System.Runtime.InteropServices in your code, otherwise, there will be compilation errors

b. Define a variable INT i = 0;

c. Call Bool State = InternetGetConnectedState (OUT I, 0);

Complete code:

using System.Runtime.InteropServices; namespace internet {public class Class1 {[DllImport ( "wininet.dll")] private extern static bool InternetGetConnectedState (out int connectionDescription, int reservedValue); public Class1 () {} private bool IsConnected () { INT i = 0; BOOL State = InternetGetConnectedState (OUT I, 0); Return State;}}}}

-----------------------------------------------

Friendly link Flying Computer Service Station Gold Caucasus Accounting Studio

转载请注明原文地址:https://www.9cbs.com/read-79486.html

New Post(0)