Ping主机以确定主机名

本文关键字:主机 Ping | 更新日期: 2023-09-27 18:25:34

嗨,我是编程新手,看不出这段代码有什么问题我有一个文本框,我将向其中添加主机名,然后添加S1字符串,然后ping。我只想让代码列出正确的主机名,所以如果我在文本框中输入名称Computer,它将ping以下

ComputerDT
ComputerLT
computerTB
computerR0

只有一个会有正确的主机名,我想把它列在列表框一中,除了最后一个if语句之外,一切都很好

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

我得到的主机1不是在本地上下文中,我不明白为什么这是错误的

有人能帮忙吗

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

        }
    }
}
}

Ping主机以确定主机名

避免使用以下构造

if(condition)
    expression;
foreach(condition)
    expression;

相反,总是使用大括号-然后您会发现问题

您需要用大括号包装foreach循环:

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

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

上面的if语句看起来也不确定{咯咯笑}:

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