C#检查网络是否连通
C#.Net检测网络在线状态
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Runtime.InteropServices;
namespace yunjson.com
{
public partial class Connected : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//True 为连通 False 为断开
bool Urlstate = IsConnected();
Response.Write(Urlstate);
}
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);
/// <summary>
/// C#检查网络是否连通
/// </summary>
/// <returns></returns>
private bool IsConnected()
{
int I = 0;
bool state = InternetGetConnectedState(out I, 0);
return state;
}
}
}