检测网络是否连接的两种方法(C#)
2009-03-27 17:49
691 查看
如何去检测网络的连接状态(C#)
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;
}
}
}
ping的函数代码
private void button2_Click(object sender, EventArgs e)
{
System.Net.NetworkInformation.Ping p = new System.Net.NetworkInformation.Ping();
System.Net.NetworkInformation.PingOptions options = new System.Net.NetworkInformation.PingOptions();
options.DontFragment = true;
string data = "abc";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 1000;
System.Net.NetworkInformation.PingReply reply = p.Send("www.google.com", timeout, buffer, options);
MessageBox.Show(reply.Status.ToString());
}
返回值:Success 则为通
相关文章推荐
- C#检测当前用户网络是否连接
- C#检查网络是否连接的方法
- C#检查网络是否连接的方法
- C#快速检测是否有可用的网络连接存在
- C#检查网络是否连接的方法
- 检测网络连接是否正常的方法
- C#判断检测网络是否连接
- C#检测当前用户网络是否连接
- Visual Basic.net 检测网络连接状态的两种方法
- c#检测网络连接问题我没有看到好的方法(转)
- iOS 检测网络状态的两种方法
- linux 下获取当前网络连接状态的两种方法
- [原创]Coding4Fun检测你的网络,用C#获取本机TCP、UDP状态及连接(一)
- 在C#中使用sqlClient和Odbc两种连接方法调用存储过程简述
- android 检测网络是否连接,或者GPS是否可用
- Acrobat 版本校验异常,请检查网络连接是否正常:NotAllowedError;安全性设置禁止访问本属性或方法。
- 网络连接状态检测的一个方法
- win平台下, 检测网络是否连接最好的办法
- C#读写txt文件的两种方法介绍【申明:来源于网络】
- C#编程:网络连接状态的定时检测