110,545
社区成员
发帖
与我相关
我的任务
分享
//导入dll
[DllImport("wininet.dll", EntryPoint = "InternetGetConnectedState")]
//判断网络状况的方法,返回值true为连接,false为未连接
public extern static bool InternetGetConnectedState(out int conState, int reder);
//在你的button事件中写下如下代码就行
private void btnNetStatus_Click(object sender, EventArgs e)
{
int n =0;
if (InternetGetConnectedState(out n,0))
{
MessageBox.Show("网终处于连接状态");
}
else
{
MessageBox.Show("网络处于未连接状态");
}
}
System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
System.Net.NetworkInformation.PingReply reply = ping.Send("www.baidu.com");
System.Net.NetworkInformation.IPStatus ipstatus = reply.Status;
//如果ipstatus为success是连网,否则未连网
Thread op = new Thread(new ThreadStart(YourPing));
op.IsBackground = true;
op.Start();