using System; using System.Collections.Generic; using System.Text; using System.Net.NetworkInformation; using System.Threading;
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Ping p = new Ping();//创建Ping对象p PingReply pr = p.Send("123.145.68.1");//向指定IP或者主机名的计算机发送ICMP协议的ping数据包
if (pr.Status == IPStatus.Success)//如果ping成功 { Console.WriteLine("网络连接成功, 执行下面任务..."); } else { int times = 0;//重新连接次数; do { if (times >= 12) { Console.WriteLine("重新尝试连接超过12次,连接失败程序结束"); return; }
以下是一个判断网络是否连接的类,通过ping某一个网络地址,如某台设备的iP、网关或者是网站地址,从而判断网络是否连接、判断和某台设备是否连接、判断某个网站是否还可以访问等。 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.NetworkInformation; using System.Net; namespace Test { class Networkstate { /// <summary> /// 测试与某个网络地址是否连通 /// </summary> /// <param name="ipAddress">iP地址如:192.168.1.1,或者是网站地址如:
www.url.com
</param> /// <returns>true为连通,false为不连通</returns> public bool NetworkC(string ipAddress) { bool isConnet = false; System.Net.NetworkInformation.Ping pingSender = new System.Net.NetworkInformation.Ping(); PingOptions options = new PingOptions(); options.DontFragment = true; string data = ""; //ping内容 byte[] buffer = Encoding.UTF8.GetBytes(data); int timeout = 120; //响应时间,毫秒 try { PingReply reply = pingSender.Send(ipAddress, timeout, buffer, options); string info = ""; info = reply.Status.ToString(); if (info.Equals("Success")) { isConnet = true; } else { isConnet = false; } } catch(Exception ex){ isConnet = false; throw ex; } return isConnet; }
} }
为你唱起小乌龟
2025-06-27 05:01:51
试试这个看: using System.Runtime.InteropServices;
private const long INTERNET_CONNECTION_MODEM = 1;//Local system uses a modem to connect to the Internet. private const long INTERNET_CONNECTION_LAN = 2; //Local system uses a local area network to connect to the Internet. private const long INTERNET_CONNECTION_PROXY = 4;//Local system uses a proxy server to connect to the Internet. private const long INTERNET_CONNECTION_MODEM_BUSY = 8; //No longer used. private const long INTERNET_CONNECTION_CONFIGURED = 64; //Local system has a valid connection to the Internet, but it might or might not be currently connected. private const long INTERNET_CONNECTION_OFFLINE = 32; // Local system is in offline mode. private const long INTERNET_RAS_INSTALLED = 16; //Local system has RAS installed.
[DllImport("wininet.dll")] public static extern bool InternetGetConnectedState(out long lpdwFlags, long dwReserved);
protected void Page_Load(object sender, EventArgs e) { long lfag; string strConnectionDev = ""; if (InternetGetConnectedState(out lfag, 0)) strConnectionDev = "网络连接正常!"; else strConnectionDev = "网络连接不可用!"; if ((lfag & INTERNET_CONNECTION_OFFLINE) > 0) strConnectionDev += "OFFLINE 本地系统处于离线模式。"; if ((lfag & INTERNET_CONNECTION_MODEM) > 0) strConnectionDev += "Modem 本地系统使用调制解调器连接到互联网。"; if ((lfag & INTERNET_CONNECTION_LAN) > 0) strConnectionDev += "LAN 本地系统使用的局域网连接到互联网。"; if ((lfag & INTERNET_CONNECTION_PROXY) > 0) strConnectionDev += "a Proxy"; if ((lfag & INTERNET_CONNECTION_MODEM_BUSY) > 0) strConnectionDev += "Modem but modem is busy"; this.Response.Write("<script>alert('" + strConnectionDev + "');</script>");
}
我试过了,测试网络连接可以的
咱情种
2025-06-27 10:03:24
判断网络是否连接要使用Windows API 发个例子给你. 这个是DotNet平台的P/Invoke调用. 仿造这个,在你程序里弄个新线程,10分钟执行一次,12次退出,很容易的. private const int INTERNET_CONNECTION_MODEM = 1; private const int INTERNET_CONNECTION_LAN = 2; [DllImport("winInet.dll")] private static extern bool InternetGetConnectedState( ref int dwFlag, int dwReserved ); //调用的方法(Winform为例,放一个按钮,单击即可): private void button1_Click(object sender, System.EventArgs e){ System.Int32 dwFlag = new int(); if(!InternetGetConnectedState(ref dwFlag, 0)) MessageBox.Show("未连网!"); else if((dwFlag & INTERNET_CONNECTION_MODEM)!=0) MessageBox.Show("采用调治解调器上网。"); else if((dwFlag & INTERNET_CONNECTION_LAN)!=0) MessageBox.Show("采用网卡上网。"); } 楼上的方案属于非完全性方案,判断发包是否成功.这样永远都会有Bug的.