Partager via


IPAddress.ToString Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Converts an Internet address to its standard notation.

Namespace:  System.Net
Assembly:  System.Net (in System.Net.dll)

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
Public Overrides Function ToString As String
[SecuritySafeCriticalAttribute]
public override string ToString()

Return Value

Type: System.String
A string that contains the IP address in either IPv4 dotted-quad or in IPv6 colon-hexadecimal notation.

Exceptions

Exception Condition
SocketException

An error prevented the IPv6 address from being converted to a string.

Remarks

The ToString method converts the IP address that is stored in the Address property to either IPv4 dotted-quad or IPv6 colon-hexadecimal notation.

Examples

' Gets the IP loopback address and converts it to a string.
Dim IpAddressString As [String] = IPAddress.Loopback.ToString()
outputBlock.Text &= "Loopback IP address: "
outputBlock.Text &= IpAddressString
outputBlock.Text &= vbCrLf
   // Gets the IP loopback address and converts it to a string.
   string LoopbackIpAddressString = IPAddress.Loopback.ToString();

   outputBlock.Text += "Loopback IP address: ";
   outputBlock.Text += LoopbackIpAddressString;
   outputBlock.Text += "\n";

' This sample prints the value of the current host loopback address in  
' standard compressed format. 
   Try
      ' Get the loopback address.
      Dim loopBack As IPAddress = IPAddress.IPv6Loopback

      ' Transform the loop-back address to a string using the overloaded
      ' ToString() method. Note that the resulting string is in the compact 
      ' form: "::1".
      Dim ipv6LoopBack As String = loopBack.ToString()
      outputBlock.Text &= "The IPv6 Loopback address is: "
      outputBlock.Text &= ipv6LoopBack
      outputBlock.Text &= vbCrLf
   Catch e As Exception
      outputBlock.Text &= "Exception: "
      outputBlock.Text &= e.ToString()
      outputBlock.Text &= vbCrLf
   End Try
// This method displays the value of the current host loopback address in  
// standard compressed format. 
    try
    { 
      // Get the loopback address.
      IPAddress loopBack = IPAddress.IPv6Loopback;

      // Transform the loop-back address to a string using the overladed
      // ToString() method. Note that the resulting string is in the compact 
      // form: "::1".
      string ipv6LoopBack = loopBack.ToString();

      outputBlock.Text += "The IPv6 Loopback address is: ";
      outputBlock.Text += ipv6LoopBack;
      outputBlock.Text += "\n";

    }
    catch (Exception e) 
    {
      outputBlock.Text += "Exception: ";
      outputBlock.Text += e.ToString();
      outputBlock.Text += "\n";
    }

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.