Returns the directory name component of the specified path string.
[Edit]
Parameters
- path
- A string containing the path of a file or directory.
[Edit]
Returns
A string containing directory information for path, or
null if path denotes a root directory, is
the empty string, or is null. Returns string.Empty if path
does not contain directory information.
[Edit]
Exceptions
Remarks
Example
The following example demonstrates using the Path.GetDirectoryName(string) method on a Windows system.
C# Example |
using System;
using System.IO;
class GetDirectoryTest {
public static void Main() {
string [] paths = {
@"\ecmatest\examples\pathtests.txt",
@"\ecmatest\examples\",
"pathtests.xyzzy",
@"\",
@"C:\",
@"\\myserver\myshare\foo\bar\baz.txt"
};
foreach (string pathString in paths) {
string s = Path.GetDirectoryName(pathString);
Console.WriteLine("Path: {0} directory is {1}",pathString, s== null? "null": s);
}
}
}
|
The output is
Path: \ecmatest\examples\pathtests.txt directory is \ecmatest\examples
Path: \ecmatest\examples\ directory is \ecmatest\examples
Path: pathtests.xyzzy directory is
Path: \ directory is null
Path: C:\ directory is null
Path: \\myserver\myshare\foo\bar\baz.txt directory is \\myserver\myshare\foo\bar
Requirements
Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0