IPAddress.Address Property
Note: This API is now obsolete.
An Internet Protocol (IP) address.
Assembly: System (in System.dll)
| Exception | Condition |
|---|---|
| SocketException |
The address family is InterNetworkV6. |
This property is obsolete. Use GetAddressBytes.
To convert Address to dotted-quad notation, use the ToString method.
The following example uses the Address parameter to retrieve the IP address of the IPAddress instance.
public void PrintAddress(String IpAddressString) { // Creates an instance of the IPAddress for the specified IP string in // dotted-quad notation. IPAddress hostIPAddress = IPAddress.Parse(IpAddressString); Console.WriteLine("\nThe IP address '" + IpAddressString + "' is {0}", hostIPAddress.ToString()); }
.NET Framework
Supported in: 1.0Obsolete (compiler warning) in 4
Obsolete (compiler warning) in 3.5
Obsolete (compiler warning) in 3.5 SP1
Obsolete (compiler warning) in 3.0
Obsolete (compiler warning) in 3.0 SP1
Obsolete (compiler warning) in 3.0 SP2
Obsolete (compiler warning) in 2.0
Obsolete (compiler warning) in 2.0 SP1
Obsolete (compiler warning) in 2.0 SP2
Obsolete (compiler warning) in 1.1
.NET Framework Client Profile
Obsolete (compiler warning) in 4Obsolete (compiler warning) in 3.5 SP1
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
- 2/23/2012
- ChipMcCaslin
Forget about the built in functions because you will be forced into more silly scenarios such as network byte order, etc. that is designed to eventually make you give up out of frustration!
Solution: (Take note of that word, MS! s-o-l-u-t-i-o-n) After getting the dotted IP address, run it through this function and you should have the correct numeric long value that (backwards thinking .net will understand):
' Use this for .net apps such as socket binding, etc. (the one at the bottom is for the rest of the computer world.)
Public Function IPAddressToNumber_DOTNETSTYLE(ByVal IP_StrValue As String) As Long
Try
Dim IPAddress As IPAddress = IPAddress.Parse(IP_StrValue)
With IPAddress
Dim IPbyte0 As Long = Convert.ToInt32(IPAddress.GetAddressBytes(3))
Dim IPbyte1 As Long = Convert.ToInt32(IPAddress.GetAddressBytes(2))
Dim IPbyte2 As Long = Convert.ToInt32(IPAddress.GetAddressBytes(1))
Dim IPbyte3 As Long = Convert.ToInt32(IPAddress.GetAddressBytes(0))
Return ((256 ^ 3) * IPbyte0) + ((256 ^ 2) * IPbyte1) + (256 * IPbyte2) + (IPbyte3)
End With
Catch ex As Exception
Return 0L
End Try
End Function
' For a Normal Logical Address conversion(which will be backwards in .net but correct for every other scenario in the universe),
' one would actually reverse the byte calculations like the following.
Public Function IPAddressToNumber(ByVal IP_StrValue As String) As Long
Try
Dim IPAddress As IPAddress = IPAddress.Parse(IP_StrValue)
With IPAddress
Dim IPbyte0 As Long = Convert.ToInt32(IPAddress.GetAddressBytes(0))
Dim IPbyte1 As Long = Convert.ToInt32(IPAddress.GetAddressBytes(1))
Dim IPbyte2 As Long = Convert.ToInt32(IPAddress.GetAddressBytes(2))
Dim IPbyte3 As Long = Convert.ToInt32(IPAddress.GetAddressBytes(3))
Return ((256 ^ 3) * IPbyte0) + ((256 ^ 2) * IPbyte1) + (256 * IPbyte2) + (IPbyte3)
End With
Catch ex As Exception
Return 0L
End Try
End Function
' I could list literally thousands of these pitfalls that were created by ms and have never been fixed even since the first .net framework.
private bool NotInRange(string ip, string ipStart, string ipEnd)
{
bool result = false;
System.Net.IPAddress iStart = System.Net.IPAddress.Parse(ipStart);
System.Net.IPAddress iEnd = System.Net.IPAddress.Parse(ipEnd);
System.Net.IPAddress iToCheck = System.Net.IPAddress.Parse(ip);
if (iStart.Address <= iToCheck.Address && iEnd.Address >= iToCheck.Address)
{
result = true;
}
return result;
}
- 8/26/2011
- JP Hellemons
I'm not entirely convinced this should be marked as obsolete: it's still extremely useful for calculating network ranges based on CIDR notation or when given a subnet mask in the form of an IP address:
IPAddress ip = IPAddress.Parse("192.168.1.151");
IPAddress subnetMask = IPAddress.Parse("255.255.255.192");
IPAddress networkId = new IPAddress(ip.Address & subnetMask.Address);
Console.WriteLine(networkId.ToString()); // will print 192.168.1.128
- 9/13/2010
- Trueborn