2012-08-03 10 views
57

Wenn ich Ping ein Remote-System mit Fenstern es sagt, es gibt keine Antwort, aber wenn ich ping mit C# sagt, es Erfolg. Windows ist korrekt, das Gerät ist nicht verbunden. Warum kann mein Code erfolgreich pingen, wenn Windows nicht funktioniert?Mit ping in C#

Hier ist mein Code:

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

Überprüfen Sie das folgende Beispiel am unteren Ende dieser Seite veröffentlicht, wenn Sie auf der MSDN-Link klicken http://msdn.microsoft .com/en-us/library/system.net.networkinformation.ping.aspx oder http://stackoverflow.com/questions/1281176/making-a-ping-inside-of-my-c-sharp-application – MethodMan

+5

Sie sollten PR.Status mit IPStatus.S vergleichen Zugriff. Stringvergleich ist in diesem Fall nicht das richtige Werkzeug. –

+0

Nachdem Sie Ihre Ping ausführen, was sind die Werte von einigen der PingReply Eigenschaften (wie 'PR.Address',' PR.RoundtripTime', 'PR.reply.Buffer.Length' und' PR.Options.Ttl') Sind Sie sicher, dass Sie die richtige IP-Adresse in Ihrem Code und keine Test-IP-Adresse haben? –

Antwort

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

Kudos für die Verwendung der Referenz! – mattpm

-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(); 
}