开发者

Ip Address Parse Problem in C#

开发者 https://www.devze.com 2023-01-20 15:30 出处:网络
I am reading IPaddress from xml file and I put it into IPaddress.parse() and then use it but it does not work. it says \"An invalid IP address was specified.\" but when I write it manually it works.

I am reading IPaddress from xml file and I put it into IPaddress.parse() and then use it but it does not work. it says "An invalid IP address was specified." but when I write it manually it works.

why I cant use IP address after reading xml file. I tried to erase "white spaces" , it said samething again.

string ipadd; //take ip address from xml and use
 ...
 IPAddress ipaddre = IPAddress.Parse(ipadd); 
---------开发者_如何学Go---------------------
 IPAddress ipaddre = IPAddress.Parse("255.255.255.255")

why these are not giving same result?


If ipadd == "255.255.255.255" then there should be nothing stopping that from working.

Obviously, though, ipadd != "255.255.255.255"

I would suggest debugging, setting a breakpoint, and inspecting the value of ipadd when you pass it to the IPAddress.Parse() method.


On the assumption that ipadd = "255.255.255.2552", then only two possibilities exist:

1) An exception is being thrown on the parse, and therefore your compare is not happening, or 2) The method you are using to compare the two results is not appropriate.

I strongly suggest you add both statements and place a breakpoint on the next line, then you will see exactly what you are dealing with:

IPAddress ipaddreReal = IPAddress.Parse(ipadd);
IPAddress ipaddreFake = IPAddress.Parse("255.255.255.255");
bool result = ipAddre.Real.Equals(ipaddreFake);
0

精彩评论

暂无评论...
验证码 换一张
取 消