首页 > 开发 > 综合 > 正文

如何去检测网络的连接状态(C#)

2024-07-21 02:24:58
字体:
来源:转载
供稿:网友
菜鸟学堂:
1. 方法定义

[dllimport("wininet.dll")]

private extern static bool internetgetconnectedstate( out int connectiondescription, int reservedvalue ) ;

2. 方法说明

参数:

connectiondescription : 连接说明

reservedvalue : 保留值

返回值:

true: on line

false: off line

3. 调用方法

a. 你必须在你的code里引用system.runtime.interopservices,否则,会有编译错误

b. 定义一个变量 int i = 0;

c. 调用bool state = internetgetconnectedstate(out i,0);



完整的代码:

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;

}

}

}




发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表