Uri.TryCreate Method (String, UriKind, Uri%)

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

Creates a new Uri using the specified String instance and a UriKind.

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

Syntax

'Declaration
Public Shared Function TryCreate ( _
    uriString As String, _
    uriKind As UriKind, _
    <OutAttribute> ByRef result As Uri _
) As Boolean
public static bool TryCreate(
    string uriString,
    UriKind uriKind,
    out Uri result
)

Parameters

  • result
    Type: System.Uri%
    When this method returns, contains the constructed Uri.

Return Value

Type: System.Boolean
A Boolean value that is true if the Uri was successfully created; otherwise, false.

Remarks

If this method returns true, the new Uri is in result.

Silverlight-based applications are cross-platform, so they run in most modern Web browsers, including Apple Safari version 2.0 and later on Apple Mac OS X. However, TryCreate(String, UriKind, Uri%) does not convert backslashes in the uriString parameter to forward slashes in the Uri on Apple Mac OS X.

Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows CE Platform Note: The .NET Compact Framework does not differentiate between relative and absolute paths. The uriKind parameter is not evaluated.

Examples

' String to create.
Dim addressString As String = "catalog/shownew.htm?date=today"
' Parse the string and create a new Uri instance, if possible.
Dim result As Uri = Nothing
If Uri.TryCreate(addressString, UriKind.RelativeOrAbsolute, result) Then
  ' The call was successful. Write the URI address to the console.
  outputBlock.Text &= result.ToString()

  ' Check whether new Uri instance is absolute or relative.
  If result.IsAbsoluteUri Then 
     outputBlock.Text &= " is an absolute Uri."
     outputBlock.Text &= vbCrLf
  Else
     outputBlock.Text += " is a relative Uri"
     outputBlock.Text &= vbCrLf
  End If    
Else   
  ' Let the user know that the call failed.
  outputBlock.Text &= "addressString could not be parsed as a URI "
  outputBlock.Text &= "address."
  outputBlock.Text &= vbCrLf
End If
// String to create.
string addressString = "catalog/shownew.htm?date=today";
// Parse the string and create a new Uri instance, if possible.
Uri result = null;
if (Uri.TryCreate(addressString, UriKind.RelativeOrAbsolute, out result) == true) {
   // The call was successful. Write the URI address to the console.
   outputBlock.Text += result.ToString();

   // Check whether new Uri instance is absolute or relative.
   if (result.IsAbsoluteUri) {
      outputBlock.Text += " is an absolute Uri.\n";
   }
   else {
      outputBlock.Text += " is a relative Uri.\n";
   }
}
else {
   // Let the user know that the call failed.
   outputBlock.Text += "addressString could not be parsed as a URI ";
   outputBlock.Text += "address.\n";
}

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

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