System.Uri.GetLeftPart Method

Gets the specified portion of a Uri instance.

Syntax

public string GetLeftPart (UriPartial part)

Parameters

part
One of the UriPartial values that specifies the end of the URI portion to return.

Returns

A string that contains the specified portion of the Uri instance.

Exceptions

TypeReason
ArgumentExceptionThe part parameter is not a valid UriPartial value.

Remarks

The Uri.GetLeftPart(UriPartial) method returns a string containing the leftmost portion of the URI string, ending with the portion specified by part.

Uri.GetLeftPart(UriPartial) includes delimiters in the following cases:

The following examples show a URI and the results of calling Uri.GetLeftPart(UriPartial) with UriPartial.Scheme, UriPartial.Authority, UriPartial.Path, or UriPartial.Query.

http://www.contoso.com/index.htm?date=today

http://

http://www.contoso.com

http://www.contoso.com/index.htm

http://www.contoso.com/index.htm?date=today

http://www.contoso.com/index.htm#main

http://

http://www.contoso.com

http://www.contoso.com/index.htm

http://www.contoso.com/index.htm

mailto:user@contoso.com?subject=uri

mailto:

mailto:user@contoso.com

mailto:user@contoso.com?subject=uri

<none>

nntp://news.contoso.com/123456@contoso.com

nntp://

nntp://news.contoso.com

nntp://news.contoso.com/123456@contoso.com

nntp://news.contoso.com/123456@contoso.com

news:123456@contoso.com

news:

news:123456@contoso.com

news:123456@contoso.com

<none>

file://server/filename.ext

file://

file://server

file://server/filename.ext

file://server/filename.ext

Example

The following example demonstrates the Uri.GetLeftPart(UriPartial) method.

C# Example

using System;

public class UriTest {
  public static void Main() {
    string[] myUri  = {
            "http://www.contoso.com/index.htm",
            "http:www.contoso.com/index.htm#mark",
                    "mailto:user@contoso.com?subject=uri",
                    "nntp://news.contoso.com/123456@contoso.com"
    };
    foreach (string s in myUri) {
      Uri aUri = new Uri(s);
      Console.WriteLine("URI: {0}", aUri.ToString());
      Console.WriteLine("Scheme: {0}",aUri.GetLeftPart(UriPartial.Scheme));
      Console.WriteLine("Authority: {0}",aUri.GetLeftPart(UriPartial.Authority));
      Console.WriteLine("Path: {0}",aUri.GetLeftPart(UriPartial.Path));
    }  
  }
}

The output is

URI: http://www.contoso.com/index.htm
Scheme: http://
Authority: http://www.contoso.com
Path: http://www.contoso.com/index.htm
URI: http://www.contoso.com/index.htm#mark
Scheme: http://
Authority: http://www.contoso.com
Path: http://www.contoso.com/index.htm
URI: mailto:user@contoso.com?subject=uri
Scheme: mailto:
Authority:
Path: mailto:user@contoso.com
URI: nntp://news.contoso.com/123456@contoso.com
Scheme: nntp://
Authority: nntp://news.contoso.com
Path: nntp://news.contoso.com/123456@contoso.com

Requirements

Namespace: System
Assembly: System (in System.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0