A string that contains the volume information, root information, or both for the specified path.
Type Reason 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.SecurityException The caller does not have the required permission. System.IO.PathTooLongException The length of path or the absolute path information for path exceeds the system-defined maximum length. UnauthorizedAccessException The caller does not have the required permission.
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.
Type | Reason |
---|---|
System.Security.Permissions.FileIOPermission | Requires permission to access path information for the specified file or directory. See System.Security.Permissions.FileIOPermissionAccess.PathDiscovery |
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:\