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

System.IO.Directory.GetDirectoryRoot Method

Returns the path root component of the specified path.

public static string GetDirectoryRoot (string path)

Parameters

path
A string containing the name of a file or directory.

Returns

A string containing the root information for the specified path.

Platforms that do not support this feature return string.Empty.

Permissions

TypeReason
System.Security.Permissions.FileIOPermissionRequires permission to access path information for the specified file or directory. See System.Security.Permissions.FileIOPermissionAccess.PathDiscovery

Exceptions

TypeReason
ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters.
ArgumentNullExceptionpath is null.
System.Security.SecurityExceptionThe caller does not have the required permission.
System.IO.PathTooLongExceptionThe length of path or the absolute path information for path exceeds the system-defined maximum length.
UnauthorizedAccessExceptionThe caller does not have the required permission.

Remarks

This method obtains the full path information for path, as returned by Path.GetFullPath(string) (path) and returns the path root component. The specified path is not required to exist.

The path argument is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory.

Note: To obtain the current working directory, see Directory.GetCurrentDirectory .

Example

The following example demonstrates the Directory.GetDirectoryRoot(string) method.

C# Example

using System;
using System.IO;
class GetDirectoryTest {
  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 = Directory.GetDirectoryRoot(pathString);
      Console.WriteLine("Path: {0} Directory Root is {1}",pathString, s== null? "null":s);
    }
  }
}

The output is

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

Requirements

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