Returns the path root component of the specified path.
- path
- A string containing the name of a file or directory.
A string containing the root information for the specified path.
Platforms that do not support this feature return string.Empty.
Type Reason System.Security.Permissions.FileIOPermission Requires permission to access path information for the specified file or directory. See System.Security.Permissions.FileIOPermissionAccess.PathDiscovery
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 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 .
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
Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0