Updated: January 2010
Returns the directory information for the specified path string.
Public Shared Function GetDirectoryName ( _ path As String _ ) As String
Dim path As String Dim returnValue As String returnValue = Path.GetDirectoryName(path)
public static string GetDirectoryName( string path )
public: static String^ GetDirectoryName( String^ path )
public static function GetDirectoryName( path : String ) : String
The path parameter contains invalid characters, is empty, or contains only white spaces.
The path parameter is longer than the system-defined maximum length.
In most cases, the string returned by this method consists of all characters in the path up to but not including the last DirectorySeparatorChar or AltDirectorySeparatorChar. If the path consists of a root directory, such as "c:\", null is returned. Note that this method does not support paths using "file:". Because the returned path does not include the DirectorySeparatorChar or AltDirectorySeparatorChar, passing the returned path back into the GetDirectoryName method will result in the truncation of one folder level per subsequent call on the result string. For example, passing the path "C:\Directory\SubDirectory\test.txt" into the GetDirectoryName method will return "C:\Directory\SubDirectory". Passing that string, "C:\Directory\SubDirectory", into GetDirectoryName will result in "C:\Directory".
For a list of common I/O tasks, see Common I/O Tasks.
The following code example demonstrates using the GetDirectoryName method on a Windows-based desktop platform.
Dim filepath As String = "C:\MyDir\MySubDir\myfile.ext" Dim directoryName As String Dim i As Integer = 0 While filepath <> Nothing directoryName = Path.GetDirectoryName(filepath) Console.WriteLine("GetDirectoryName('{0}') returns '{1}'", _ filepath, directoryName) filepath = directoryName If i = 1 filepath = directoryName + "\" ' this will preserve the previous path End If i = i + 1 End While 'This code produces the following output: ' ' GetDirectoryName('C:\MyDir\MySubDir\myfile.ext') returns 'C:\MyDir\MySubDir' ' GetDirectoryName('C:\MyDir\MySubDir') returns 'C:\MyDir' ' GetDirectoryName('C:\MyDir\') returns 'C:\MyDir' ' GetDirectoryName('C:\MyDir') returns 'C:\' ' GetDirectoryName('C:\') returns ''
string filePath = @"C:\MyDir\MySubDir\myfile.ext"; string directoryName; int i = 0; while (filePath != null) { directoryName = Path.GetDirectoryName(filePath); Console.WriteLine("GetDirectoryName('{0}') returns '{1}'", filePath, directoryName); filePath = directoryName; if (i == 1) { filePath = directoryName + @"\"; // this will preserve the previous path } i++; } /* This code produces the following output: GetDirectoryName('C:\MyDir\MySubDir\myfile.ext') returns 'C:\MyDir\MySubDir' GetDirectoryName('C:\MyDir\MySubDir') returns 'C:\MyDir' GetDirectoryName('C:\MyDir\') returns 'C:\MyDir' GetDirectoryName('C:\MyDir') returns 'C:\' GetDirectoryName('C:\') returns '' */
String^ filePath = "C:\\MyDir\\MySubDir\\myfile.ext"; String^ directoryName; int i = 0; while (filePath != nullptr) { directoryName = Path::GetDirectoryName(filePath); Console::WriteLine("GetDirectoryName('{0}') returns '{1}'", filePath, directoryName); filePath = directoryName; if (i == 1) { filePath = directoryName + "\\"; // this will preserve the previous path } i++; } /* This code produces the following output: GetDirectoryName('C:\MyDir\MySubDir\myfile.ext') returns 'C:\MyDir\MySubDir' GetDirectoryName('C:\MyDir\MySubDir') returns 'C:\MyDir' GetDirectoryName('C:\MyDir\') returns 'C:\MyDir' GetDirectoryName('C:\MyDir') returns 'C:\' GetDirectoryName('C:\') returns '' */
var fileName : String = "C:\\mydir\\myfile.ext"; var path : String = "C:\\mydir\\"; var rootPath : String = "C:\\"; var directoryName : String; directoryName = Path.GetDirectoryName(fileName); Console.WriteLine("GetDirectoryName('{0}') returns '{1}'", fileName, directoryName); directoryName = Path.GetDirectoryName(path); Console.WriteLine("GetDirectoryName('{0}') returns '{1}'", path, directoryName); directoryName = Path.GetDirectoryName(rootPath); Console.WriteLine("GetDirectoryName('{0}') returns '{1}'", rootPath, directoryName); /* This code produces the following output: GetDirectoryName('C:\mydir\myfile.ext') returns 'C:\mydir' GetDirectoryName('C:\mydir\') returns 'C:\mydir' GetDirectoryName('C:\') returns '' */
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune
Date
History
Reason
January 2010
Corrected return value.
Customer feedback.
System.IO.Path.GetDirectoryName(strFullFilePath.Replace(strFileName,