C#如何将此字符串[](从文本框)转换为列表或IP地址数组

时间:2014-08-10 12:07:34

标签: c# winforms list

好的,我不知道如何做到这一点,因为我正在尝试自学C#并同时创建一个工作程序。

我正在创建一个Windows Form C#项目,我的文本中包含从1个IP地址到数千个IP地址的列表。当我单击表单上的提交按钮时,我希望它一次一行地从消息框的内容创建一个列表。

我还想使用System.Net IPAddress Class将列表解析为IP地址数组,并将其解析为每个IP地址的数组。

当我使用List list = new List(textBox1.Lines)时,我没有错误;使用以下代码:

        private void Submit_Button_Click(object sender, EventArgs e)
        {
//            //Pass Text of TextBox1 to String Array tempStr
            List<string> list = new List<string>(textBox1.Lines);

//           // Loop through the array and send the contents of the array to debug window. 
//           for (int counter=0; counter < list.Count; counter++)
//           {
//               System.Diagnostics.Debug.WriteLine(list[counter]);
//           }


            this.Hide(); 
            Form2 f2 = new Form2(list);
            f2.Show();


        }

但是,如果我尝试使用IPAddress.Parse解析列表,则会引发一些错误。 列表列表=新列表(IPAddress.Parse(textBox1.Lines));

  1. 最佳重载方法匹配&#39; System.Net.IPAddress.Parse(string)&#39;有一些无效的论点
  2. Argument1:无法转换为&#39; string []&#39;到&#39;字符串&#39;
  3. 最佳重载方法匹配&#39; System.Collections.Generic.List.List(System.Collections.Generic.IEnumerable)&#39;有一些无效的论点
  4. Argument1:无法转换为System.Net.IPAddress&#39;到&#39; System.Collections.Generic.IEnumerable&#39;
  5. 我的印象是,IPAddress.Parse(textBox1.Lines)的最终产品将是一个每个3字节的4字符串数组,所以字符串数组不会工作吗?

            private void Submit_Button_Click(object sender, EventArgs e)
            {
    //            //Pass Text of TextBox1 to String Array tempStr
                List<string[]> list = new List<string[]>(IPAddress.Parse(textBox1.Lines));
    
    //           // Loop through the array and send the contents of the array to debug window. 
    //           for (int counter=0; counter < list.Count; counter++)
    //           {
    //               System.Diagnostics.Debug.WriteLine(list[counter]);
    //           }
    
    
                this.Hide(); 
                Form2 f2 = new Form2(list);
                f2.Show();
    
    
            }
    

    然后我为我的列表尝试了一种不同的变量,然后它也没有工作。 列表列表=新列表(textBox1.Lines);

    得到这些错误。 1.最佳重载方法匹配&#39; System.Collections.Generic.List.List(System.Collections.Generic.IEnumerable)&#39;有一些无效的论点 2. Argument1:无法转换为&#39; string []&#39;到&#39; System.Collections.Generic.IEnumerable&#39;

            private void Submit_Button_Click(object sender, EventArgs e)
            {
    //            //Pass Text of TextBox1 to String Array tempStr
                List<IPAddress> list = new List<IPAddress>(textBox1.Lines);
    
    //           // Loop through the array and send the contents of the array to debug window. 
    //           for (int counter=0; counter < list.Count; counter++)
    //           {
    //               System.Diagnostics.Debug.WriteLine(list[counter]);
    //           }
    
    
                this.Hide(); 
                Form2 f2 = new Form2(list);
                f2.Show();
    
    
            }
    

    我无法为我的生活找出如何将此字符串[] textBox1.Lines转换为IPAddress用于我的目的。

    请帮忙。

1 个答案:

答案 0 :(得分:1)

创建一个列表,迭代数组,然后逐个解析IP地址:

List<IPAddress> addresses = new List<IPAddress>();
foreach (string input in this.textBox1.Lines)
{
    IPAddress ip;
    if (IPAddress.TryParse(input, out ip))
    {
        addresses.Add(ip);
    }
    else
    {
        Console.WriteLine("Input malformed: {0}", input);
    }
}