方法一: 已經證實能用的.
using System;<br />using System.Collections.Generic;<br />using System.ComponentModel;<br />using System.Data;<br />using System.Drawing;<br />using System.Text;<br />using System.Windows.Forms;<br />using System.Net;<br />using System.Net.NetworkInformation;</p><p>namespace PingIpAddress<br />{<br /> public partial class Form1 : Form<br /> {<br /> public Form1()<br /> {<br /> InitializeComponent();<br /> }</p><p> private void Form1_Load(object sender, EventArgs e)<br /> {</p><p> }<br /> private Ping pingSender = new Ping();<br /> private string strIP = "";<br /> private void button1_Click(object sender, EventArgs e)<br /> {<br /> strIP = txtIP.Text; //要ping的IP地址 第一種方法本人親自實驗過。可以使用<br /> PingOptions pingOption = new PingOptions();<br /> pingOption.DontFragment = true;</p><p> string data = "sendData:goodgoodgoodgoodgoodgood";<br /> byte[] buffer = Encoding.ASCII.GetBytes(data);<br /> int timeout = 120;<br /> PingReply reply = pingSender.Send(strIP, timeout, buffer);<br /> if (reply.Status == IPStatus.Success)<br /> {<br /> MessageBox.Show("能ping通 ");<br /> }<br /> else<br /> {<br /> MessageBox.Show("ping不通");<br /> }<br /> }<br /> }<br />}</p><p>
第二種方法: 沒試過
C#判斷網路狀態<br />代碼<br />/// <summary><br />/// 是否能 Ping 通指定的主機<br />/// </summary><br />/// <param name="ip">ip 地址或主機名稱或網域名稱</param><br />/// <returns>true 通,false 不通</returns><br />public bool Ping(string ip)<br />{<br /> int timeout = 1000;<br /> string data = "Test Data!";<br /> System.Net.NetworkInformation.Ping p = new System.Net.NetworkInformation.Ping();<br /> System.Net.NetworkInformation.PingOptions options = new System.Net.NetworkInformation.PingOptions();<br /> options.DontFragment = true; byte[] buffer = Encoding.ASCII.GetBytes(data);<br /> System.Net.NetworkInformation.PingReply reply = p.Send(ip, timeout, buffer, options);<br /> if (reply.Status == System.Net.NetworkInformation.IPStatus.Success) return true; else return false;<br />}</p><p>