将字符串数组值转换为IP地址C#

时间:2015-01-14 22:58:02

标签: c# arrays string

我正在处理一个小项目而且我遇到了问题。我设置它,以便用户可以将一系列IP地址粘贴到多行文本框中并ping每个IP。我目前正在将输入框中输入的每个值添加到字符串数组中。我遇到的问题是使用IPAddress.Parse方法将该数组中的每个值转换为IP。任何提示都会受到极大的关注。它在c#

using System;
using System.Windows.Forms;
using System.Net.NetworkInformation;
using System.Net;


namespace MultiPing
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }


    private void pingBtn_Click(object sender, EventArgs e)
    {
        try
        {
            int i;
            string[] allLines = inputBox.Text.Split('\n');
            Ping pingSender = new Ping();
            for (i = 0; i < allLines.Length; i++)
            {
                try
                {
                    IPAddress address = IPAddress.Parse(allLines[]);
                    PingReply reply = pingSender.Send(address);

                    if (reply.Status == IPStatus.Success)
                    {
                        outputBox.Text = address + " is up \n";
                    }
                    else
                    {
                        outputBox.Text = address + " is down \n";
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            }

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }


    }
}
}

1 个答案:

答案 0 :(得分:2)

更改此

IPAddress address = IPAddress.Parse(allLines[]);

IPAddress address = IPAddress.Parse(allLines[i]);