SendPingAsync使应用程序进入,如果所有主机未到达C#

SendPingAsync使应用程序进入,如果所有主机未到达C#

问题描述:

我在写我的第一个应用程序之一休息方式,我已经遇到了一个问题。当我的foreach循环退出后输入无法访问的地址时,我的应用程序进入中断模式,但是如果foreach得到结果,它将正常进行。SendPingAsync使应用程序进入,如果所有主机未到达C#

这是抛出异常的代码的一部分:

private async void Button_Click(object sender, RoutedEventArgs e) 
{ 
    /// Verify that input box is not blank 
    if (string.IsNullOrWhiteSpace(inputBox.Text)) 
    { 
     MessageBox.Show("Cannot be left blank !", "Error"); 
     return; 
    } 

    /// Creates a list of Gateway options to try from 
    string[] gatewayArray = { "cadg0", "frdg0", "dedg0", "gbdg0", "iedg0", "usdg0", "dg", "dg0" }; 

    /// Specifies where to get store number from 
    string storeNumber = inputBox.Text; 
    string pingReply; 
    string pingStatus; 

    clearButton_Click(sender, e); 

    Ping ping = new Ping(); 

    foreach (string gateway in gatewayArray) 
     try 
     { 
      /// Replace store number with "Wait" text and changes color of box to red. 
      inputBox.Text = "Please Wait..."; 
      inputBox.Background = Brushes.Red; 

      /// Pings selected store using Async method 
      PingReply reply = await ping.SendPingAsync(gateway + storeNumber, 2000); 

      pingReply = reply.Address.ToString(); 
      pingStatus = reply.Status.ToString(); 

       /// Displays results of Ping 
       ipOne.Clear(); 
       ipOne.Text = pingReply; 
       statusOne.Clear(); 
       statusOne.Text = pingStatus; 
       if (statusOne.Text == "Success") 
       { 
        statusOne.Text = "- ONLINE -"; 
        statusOne.Background = Brushes.LightGreen; 
       } 
       else 
       { 
        statusOne.Background = Brushes.Orange; 
       } 

       /// Get name of host 
       IPHostEntry ipHostOne = Dns.GetHostEntry(pingReply); 
       string ipOneName = ipHostOne.HostName; 
       ipOneName = ipOneName.Substring(0, ipOneName.LastIndexOf(".") - 10); 
       nameOne.Text = ipOneName.ToUpper(); 

      } 

      catch (ArgumentOutOfRangeException ex) 
      { 
       MessageBox.Show(ex.Message); 
      } 

      catch (PingException) 
      { 
       /// Catches exceptions and continues 
       /// MessageBox.Show("Unreachable !"); 
      } 

如果我指定不正确门店数量获取:

抛出异常: 'System.ArgumentOutOfRangeException' 在mscorlib.dll 类型“System.ArgumentOutOfRangeException”的发生未处理的异常 在mscorlib.dll 开始索引不能小于零。

The program '[19616] SbPinger.exe' has exited with code -1 (0xffffffff). 

我试着用try/catch语句来处理它:

catch (ArgumentOutOfRangeException ex) 
{ 
    MessageBox.Show(ex.Message); 
} 

然而渔获什么都不做,程序依然进入中断模式。

我的计划只是执行ping所有可能的默认网关和IP返回如果找到一个,然后坪个人电脑的该网络上。除Ping没有到达任何网关外,其他所有工作都可以使用。因为我只用了几周的时间就使用了C#,所以请放轻松点。任何帮助将不胜感激。

彼得

+0

刚一说明,'string.IsNullOrWhiteSpace'还检查字符串是空的,所以'inputBox.Text ==“”'是多余的。在这里也是错误的,因为你应该首先检查null。也就是说,你也应该在这里发布异常的调用堆栈。 – Clemens

+0

得到了最新的感谢。还添加了Try/catch代码的其余部分。 –

+0

调用堆栈如何?和'ipOneName.Substring(0,ipOneName.LastIndexOf( “”) - 10)'看起来可疑。你确定总是有一个'.',并且它的位置从一开始至少有十个字符吗? – Clemens

我发现东张西望了一会儿,尝试不同的事情后让使用Dns.GetHostEntry代替查验网址,收益更快的结果,并不会崩溃了,因为所有的流程完成。这里的代码如果有人感兴趣:

// Get IP from DG adrress 
      try 
      { 
       IPHostEntry hostEntry = Dns.GetHostEntry("URL"); 
       IPAddress[] address = hostEntry.AddressList; 
       textBox.Text = address.GetValue(0).ToString(); 
      } 
      catch 
      { 
       // Catches Exception and moves on 
       // MessageBox.Show("No result", "Error"); 
      } 

感谢您的回应!