0

我正在开发一个小型 C# 项目,但是在将字符串转换为IPAddress. 这是代码:

Ping pingeage = new Ping();
String ip = tabtempsoctets1[0] 
    + "." + tabtempsoctets1[1] 
    + "." + tabtempsoctets1[2] 
    + "." + tabtempsoctets1[3];
MessageBox.Show(ip);
IPAddress adresseTest = IPAddress.Parse(ip);
boxLogs.Text = adresseTest.ToString();
PingReply reponse = pingeage.Send(adresseTest,2000);

但是 VisualStudio 引发了一个异常,告诉我我IpAddress的不是IPAddress. 为什么? tabtempoctets1是一个字符串数组,我在这里手动添加了"." What's wrong in here ?

4

3 回答 3

4

May be you have leading or trailing space(s). Otherwise it should successfully parse "127.1.1.1" try:

IPAddress adresseTest = IPAddress.Parse(ip.Trim());

You can also try IPAddress.TryParse which would not raise an exception in case parsing fails. Like:

string str = "     127.1.1.1       ";
IPAddress a;
if (IPAddress.TryParse(str.Trim(), out a))
{
    //parsing succesful
}
else
{
    //invalid string
}

You may also use string.Join to concatenate your string like:

string ip = string.Join(".", tabtempsoctets1);
于 2013-11-05T19:21:46.523 回答
1

正确设置字符串string[] tabtempsoctets1 = { "127", "1", "1", "1" };后,上面的代码似乎对我有用。

您可以尝试转换为字节数组来检查范围。

这是我在 Win7 PC 上成功使用的测试应用程序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;

namespace IPAddressTesting
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] tabtempsoctets1 = { "127", "1", "1", "1" };
        Ping pingeage = new Ping();
        //Ping pingeage = new Ping();
        String ip = tabtempsoctets1[0]
            + "." + tabtempsoctets1[1]
            + "." + tabtempsoctets1[2]
            + "." + tabtempsoctets1[3];
        Console.WriteLine(ip);
        IPAddress adresseTest = IPAddress.Parse(ip);
        Console.WriteLine(adresseTest.ToString());

        byte [] addressAsBytes = new byte[tabtempsoctets1.Length];
        for (int i = 0; i < tabtempsoctets1.Length; i++)
        {
            if (!byte.TryParse(tabtempsoctets1[i], out addressAsBytes[i]))
                Console.WriteLine(tabtempsoctets1[i] + " is not formated correctely");
        }
        IPAddress adresseTest2 = new IPAddress(addressAsBytes);
        Console.WriteLine(adresseTest2.ToString());
        PingReply reponse = pingeage.Send(adresseTest2, 2000);
        Console.WriteLine(reponse.Status.ToString());

        Console.ReadKey();
    }
}

}enter code here

于 2013-11-05T19:52:23.873 回答
0
        string[] tabtempsoctets1 = new string[] { "127", "1", "1", "1" }; 

        Ping pingeage = new Ping();
        String ip = tabtempsoctets1[0]
            + "." + tabtempsoctets1[1]
            + "." + tabtempsoctets1[2]
            + "." + tabtempsoctets1[3];
        MessageBox.Show(ip);
        IPAddress adresseTest = IPAddress.Parse(ip);
       // boxLogs.Text = adresseTest.ToString();
        PingReply reponse = pingeage.Send(adresseTest, 2000);

         works for me
于 2013-11-05T19:26:52.483 回答