How I can compare two IP address?
string ip1 = "123.123.123.123";
string ip2 = "124.124.124.124";
I need some like this:
if(ip1 == ip2)
{
//true
}
How I can compare two IP address?
string ip1 = "123.123.123.123";
string ip2 = "124.124.124.124";
I need some like this:
if(ip1 == ip2)
{
//true
}
The
IPAddress
class (System.Net) has an overridden Equals method that will compare the addresses, not the object instances, which is what you want. String comparison here may be dangerous since it is possible for IP addresses to have more than one string representation. http://msdn.microsoft.com/en-us/library/system.net.ipaddress.equals%28v=VS.71%29.aspxCheck out Equals method on
System.Net.IPAddress
You can use this class to compare IpAddress :
http://www.codeproject.com/Articles/26550/Extending-the-IPAddress-object-to-allow-relative-c
It seems System.Net.IPAddress defines it's own Equals override so this should work:
The type
IPAddress
in the BCL supports equality and can be used for this purpose.Several people have wondered why a straight string comparison is not sufficient. The reason why is that an IP address can be legally represented in both base 10 and hexidecimal notation. So the same IP address can have more than 1 string representation.
For example