CMD 下的 Ping 工具一直是很簡便的 網路連線偵測工具,本範例是使用
Net.framework 2 的 Ping 類別 來達成目的。
專案下載
private void button1_Click(object sender, EventArgs e)
{
Ping p1 = new Ping();
p1.PingCompleted += new PingCompletedEventHandler(this.PingCompletedCallBack);
if (textBox1.Text.Trim().Length == 0)
{
return;
}
p1.SendAsync(textBox1.Text.Trim(), null);
}
private void PingCompletedCallBack(object sender, PingCompletedEventArgs e)
{
listBox1.Items.Clear();
if (e.Cancelled)
{
listBox1.Items.Add("Ping Canncel");
return;
}
if (e.Error != null)
{
listBox1.Items.Add("e.Error.Message");
return;
}
PingReply reply = e.Reply;
listBox1.Items.Add(reply.Address + " " + reply.Status.ToString());
if (reply.Status == IPStatus.Success)
{
listBox1.Items.Add("RoundTrip time:" + reply.RoundtripTime);
listBox1.Items.Add("Time to live:"+reply.Options.Ttl);
listBox1.Items.Add("Don't fragment:"+reply.Options.DontFragment);
listBox1.Items.Add("Buffer size:"+reply.Buffer.Length);
}
}