Mono Class Library: System.IO.Path Overview | Members

System.IO.Path.GetPathRoot Method

Returns the path root component of the specified path. [Edit]

public static string GetPathRoot (string path)

Parameters

path
A string containing the path from which to obtain root directory information [Edit]

Returns

A string containing the root directory of path, or null if path is null . Returns string.Empty if the specified path does not contain root information.

Platforms that do not support this feature return path unmodified.

[Edit]

Exceptions

TypeReason
ArgumentExceptionpath contains one or more implementation-specific invalid characters or is equal to string.Empty . [Edit]

Remarks

This method does not verify that the path exists.

The exact behavior of this method is implementation-specific.

[Edit]

Example

The following example demonstrates the Path.GetPathRoot(string) method.

C# Example
using System;
using System.IO;
class GetPathRootTest 
{
  public static void Main() {
    string [] paths = {                                

@"\ecmatest\examples\pathtests.txt",
      "pathtests.xyzzy",
      @"\",
      @"C:\",                                  

@"\\myserver\myshare\foo\bar\baz.txt"
    };
    foreach (string pathString in paths) {
      string s = Path.GetPathRoot(pathString);
      Console.WriteLine("Path: {0} Path root is {1}",pathString, s== null? "null": s);
    }
  }
}

The output is

Path: \ecmatest\examples\pathtests.txt Path root is \
Path: pathtests.xyzzy Path root is
Path: \ Path root is \
Path: C:\ Path root is C:\
Path: \\myserver\myshare\foo\bar\baz.txt Path root is \\myserver\myshare

Requirements

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