当Ping可以';无法访问主机名C#

当Ping可以';无法访问主机名C#,c#,C#,我试着写一些东西,在一个文本框中提取输入,然后附加前缀,然后ping这两个。每次我运行脚本时,如果它找不到主机名,就会给我一个异常。是否有任何方法可以强制通过异常,使其进入else语句?还是有什么简单的东西我遗漏了 public void button1_Click(object sender, EventArgs e) { if (String.IsNullOrWhiteSpace(textBox1.Text)) { MessageBox.Show("Pleas

我试着写一些东西,在一个文本框中提取输入,然后附加前缀,然后ping这两个。每次我运行脚本时,如果它找不到主机名,就会给我一个异常。是否有任何方法可以强制通过异常,使其进入else语句?还是有什么简单的东西我遗漏了

public void button1_Click(object sender, EventArgs e)
{
    if (String.IsNullOrWhiteSpace(textBox1.Text))
    {
        MessageBox.Show("Please enter an asset tag number.", "Dramatic failure", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    }
    else
    {
        string assetTag = textBox1.Text;

        Ping pingSender = new Ping();

        string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
        byte[] buffer = Encoding.ASCII.GetBytes(data);

        int timeout = 1000;

        PingOptions options = new PingOptions(64, true);

        PingReply reply = pingSender.Send("WES0" + assetTag);

        if (reply.Status == IPStatus.Success)
        {
            MessageBox.Show("The IP address is: ", "Great sucess!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
        else
        {
        }
    }
}

使用
try/Catch
捕获该验证。比如:

try
{
    PingReply reply = pingSender.Send(nameOrAddress);

    if (reply.Status == IPStatus.Success)
    {
        MessageBox.Show("The IP address is: ", "Great sucess!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    }
    else
    {
    }
}
catch (PingException)
{
    // Discard PingExceptions
}
Ping类抛出此异常以指示在发送 Internet控制消息协议(ICMP)回显请求,一种称为 Ping类引发了一个未处理的异常。申请应 检查PingException对象的内部异常以识别 问题

如果ICMP回显,Ping类不会引发此异常 由于网络、ICMP或目标错误,请求失败。对于 如果出现此类错误,Ping类将返回带有 状态属性中设置的相关IPStatus值

查找有关PingException的详细信息。

尝试以下操作:

public void button1_Click(object sender, EventArgs e)
{
    if (String.IsNullOrWhiteSpace(textBox1.Text))
    {

        MessageBox.Show("Please enter an asset tag number.", "Dramatic failure", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    }
    else
    {
        string assetTag = textBox1.Text;

        Ping pingSender = new Ping();

        string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
        byte[] buffer = Encoding.ASCII.GetBytes(data);

        int timeout = 1000;

        PingOptions options = new PingOptions(64, true);

        try
        {            
            PingReply reply = pingSender.Send("WES0" + assetTag);

            if (reply.Status == IPStatus.Success)
            {
                MessageBox.Show("The IP address is: ", "Great sucess!", MessageBoxButtons.OK, MessageBoxIcon.Warning);

            }
            else
            {
                MessageBox.Show("Error on ping!", "Error on ping!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        catch(Exception ex)
        {
            MessageBox.Show("Error on ping:" + ex.Message, "Error on ping!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
    }
}

那么,有什么例外?您可以使用
try/catch
块捕获和处理异常。读取。作为一个全新的网站,它有助于你真正熟悉你发布的网站。。。