Ping主机确定主机名

时间:2012-02-22 22:29:13

标签: c#

您好我是编程新手,无法看到此代码有什么问题 我有一个文本框,我将添加一个主机名,然后S1字符串将添加,然后ping所有我想要的代码是列出什么是正确的主机名,所以如果我在文本框中输入名称计算机它将ping以下

ComputerDT
ComputerLT
computerTB
computerR0

只有一个人拥有正确的主机名,我想在列表框中列出一个,除了最后一个if语句之外,它的工作正常

if (host1 != "")
    listBox1.Items.Add(host1);

我得到host1不在本地环境中我不明白为什么这是错误的

任何人都可以帮忙吗

public partial class Form1 : Form
    {
        public string S1 = "DT";
        public string S2 = "LT";
        public string S3 = "R0";
        public string S4 = "TB";

    public Form1()
    {
        InitializeComponent();



    }

    private void button1_Click(object sender, EventArgs e)
    {
        update();
    }



    static string Ping(string host)
    {
        Ping pingSender = new Ping();
        PingOptions options = new PingOptions();
        string errMessage = string.Empty;
        string returnMessage;


        // Use the default Ttl value which is 128,
        // but change the fragmentation behavior.
        options.DontFragment = true;

        // Create a buffer of 32 bytes of data to be transmitted.
        string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
        byte[] buffer = Encoding.ASCII.GetBytes(data);
        int timeout = 120;
        try
        {
            PingReply reply = pingSender.Send(host, timeout, buffer, options);

            if (!(reply == null))
            {
                switch (reply.Status)
                {
                    case IPStatus.Success:
                        returnMessage = string.Format("Reply from {0}: bytes={1} time={2}ms TTL={3}", reply.Address, reply.Buffer.Length, reply.RoundtripTime, reply.Options.Ttl);
                        break;
                    case IPStatus.TimedOut:
                        returnMessage = "Connection has timed out...";
                        break;
                    default:
                        returnMessage = string.Format("Ping failed: {0}", reply.Status.ToString());
                        break;
                }
            }
            else
                returnMessage = "Connection failed for an unknown reason...";
        }
        catch (PingException ex)
        {
            returnMessage = string.Format("");
        }
        catch (SocketException ex)
        {
            returnMessage = string.Format("");
        }


        return returnMessage;






    }



    public void update()
    {


        string[] lines = textBox1.Lines;
        List<string> myCollection = new List<string>();

        string host1;


        foreach (string line in lines)
        {
            if (line.Contains(""))

                myCollection.Add(line + S1);
            myCollection.Add(line + S2);
            myCollection.Add(line + S3);
            myCollection.Add(line + S4);
        }

        myCollection.ToArray();



        {
            foreach (string val in myCollection)

                host1 = Ping(val);

            if (host1 != "")

                listBox1.Items.Add(host1);


        }
    }
}
}

2 个答案:

答案 0 :(得分:1)

避免使用以下构造

if(condition)
    expression;

foreach(condition)
    expression;

总是使用大括号 - 然后你会发现问题

答案 1 :(得分:0)

您需要将foreach循环包装在大括号中:

foreach (string val in myCollection)
{
    host1 = Ping(val);
    if (host1 != "")
        listBox1.Items.Add(host1);
}

否则只会循环第一行。 (这就是host1不再在范围内的原因)

上面的if语句看起来也是{chuckle}:

if (line.Contains(""))

    myCollection.Add(line + S1);
myCollection.Add(line + S2);
myCollection.Add(line + S3);
myCollection.Add(line + S4);

应该是

if (line.Contains(""))  // this will always be true.  Need to use a proper check here
{
    myCollection.Add(line + S1);
    myCollection.Add(line + S2);
    myCollection.Add(line + S3);
    myCollection.Add(line + S4);
}