2012-08-03 31 views
57

當我使用windows Ping遠程系統時,它說沒有回覆,但是當我用c#ping時,它表示成功。 Windows是正確的,設備沒有連接。爲什麼我的代碼能夠在Windows不成功時成功ping?在c中使用ping#

這裏是我的代碼:

Ping p1 = new Ping(); 
PingReply PR = p1.Send("192.168.2.18"); 
// check when the ping is not success 
while (!PR.Status.ToString().Equals("Success")) 
{ 
    Console.WriteLine(PR.Status.ToString()); 
    PR = p1.Send("192.168.2.18"); 
} 
// check after the ping is n success 
while (PR.Status.ToString().Equals("Success")) 
{ 
    Console.WriteLine(PR.Status.ToString()); 
    PR = p1.Send("192.168.2.18"); 
} 
+3

看看下面的例子張貼在該頁面的底部,當你點擊MSDN鏈接http://msdn.microsoft .com/en-us/library/system.net.networkinformation.ping.aspx 或 http://stackoverflow.com/questions/1281176/making-a-ping-inside-of-my-c-sharp-application – MethodMan 2012-08-03 18:05:29

+5

您應該將PR.Status與IPStatus.S進行比較uccess。在這種情況下,字符串比較不是正確的工具。 – 2012-08-03 18:09:36

+0

執行ping之後,某些PingReply屬性的值(如PR.Address,PR.RoundtripTime,PR.reply.Buffer.Length和PR.Options.Ttl) )?你是否確定你的代碼中有正確的IP地址,而不是測試IP地址? – 2012-08-03 18:25:41

回答

127
using System.Net.NetworkInformation;  

public static bool PingHost(string nameOrAddress) 
{ 
    bool pingable = false; 
    Ping pinger = new Ping(); 
    try 
    { 
     PingReply reply = pinger.Send(nameOrAddress); 
     pingable = reply.Status == IPStatus.Success; 
    } 
    catch (PingException) 
    { 
     // Discard PingExceptions and return false; 
    } 
    return pingable; 
} 
15
using System.Net.NetworkInformation; 

    namespace PingTest1 
    { 
     public partial class Form1 : Form 
     { 
      public Form1() 
      { 
       InitializeComponent(); 
      } 

      private void button1_Click(object sender, EventArgs e) 
      { 
       Ping p = new Ping(); 
       PingReply r; 
       string s; 
       s = textBox1.Text; 
       r = p.Send(s); 

       if (r.Status == IPStatus.Success) 
       { 
        lblResult.Text = "Ping to " + s.ToString() + "[" + r.Address.ToString() + "]" + " Successful" 
         + " Response delay = " + r.RoundtripTime.ToString() + " ms" + "\n"; 
       } 
      } 

      private void textBox1_Validated(object sender, EventArgs e) 
      { 
       if (string.IsNullOrWhiteSpace(textBox1.Text) || textBox1.Text == "") 
       { 
        MessageBox.Show("Please use valid IP or web address!!"); 
       } 
      } 
     } 

    } 
+7

包括使用參考的榮譽! – mattpm 2016-06-13 00:03:53

-1
private void button26_Click(object sender, EventArgs e) 
{ 
    System.Diagnostics.ProcessStartInfo proc = new System.Diagnostics.ProcessStartInfo(); 
    proc.FileName = @"C:\windows\system32\cmd.exe"; 
    proc.Arguments = "/c ping -t " + tx1.Text + " "; 
    System.Diagnostics.Process.Start(proc); 
    tx1.Focus(); 
} 

private void button27_Click(object sender, EventArgs e) 
{ 
    System.Diagnostics.ProcessStartInfo proc = new System.Diagnostics.ProcessStartInfo(); 
    proc.FileName = @"C:\windows\system32\cmd.exe"; 
    proc.Arguments = "/c ping " + tx2.Text + " "; 
    System.Diagnostics.Process.Start(proc); 
    tx2.Focus(); 
}