System.IO.Directory.GetDirectoryRoot Method

Returns the volume information, root information, or both for the specified path.

Syntax

public static string GetDirectoryRoot (string path)

Parameters

path
The path of a file or directory.

Returns

A string that contains the volume information, root information, or both for the specified path.

Exceptions

TypeReason
ArgumentException path is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters.
ArgumentNullException path 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 fully qualified path name of path, as returned by Path.GetFullPath(string), and returns root directory information. The specified path is not required to exist.

The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see Directory.GetCurrentDirectory.

The path parameter is not case-sensitive.

For a list of common I/O tasks, see Common I/O Tasks.

Permissions

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

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, 4.0.0.0