In java there is a method inetaddress.isSiteLocalAddrress to determine if the IP address is in the same range as that of site, do we have any equivalent method in C#
Asked
Active
Viewed 176 times
0
-
Essentially you're trying to check if a given IP address is private. – Rami Sakr Dec 10 '14 at 12:43
1 Answers
0
private bool isIPLocal(IPAddress ipaddress)
{
String[] straryIPAddress = ipaddress.ToString().Split(new String[] { "." }, StringSplitOptions.RemoveEmptyEntries);
int[] iaryIPAddress = new int[] { int.Parse(straryIPAddress[0]), int.Parse(straryIPAddress[1]), int.Parse(straryIPAddress[2]), int.Parse(straryIPAddress[3]) };
if (iaryIPAddress[0] == 10 || (iaryIPAddress[0] == 192 && iaryIPAddress[1] == 168) || (iaryIPAddress[0] == 172 && (iaryIPAddress[1] >= 16 && iaryIPAddress[1] <= 31)))
{
return true;
}
else
{
// IP Address is "probably" public. This doesn't catch some VPN ranges like OpenVPN and Hamachi.
return false;
}
}

Rami Sakr
- 556
- 1
- 6
- 14
-
Answer originally from: [http://stackoverflow.com/questions/8113546/how-to-determine-whether-an-ip-address-in-private](http://stackoverflow.com/questions/8113546/how-to-determine-whether-an-ip-address-in-private) – Rami Sakr Dec 10 '14 at 12:44
-
I was looking for some inbuilt function same as IsSiteLocalAddrress, but this will also do the task. – user1010186 Dec 11 '14 at 05:07