Path.GetDirectoryName Path.GetDirectoryName Path.GetDirectoryName Path.GetDirectoryName Method

Definition

Overloads

GetDirectoryName(String) GetDirectoryName(String) GetDirectoryName(String) GetDirectoryName(String)

Returns the directory information for the specified path string.

GetDirectoryName(ReadOnlySpan<Char>) GetDirectoryName(ReadOnlySpan<Char>) GetDirectoryName(ReadOnlySpan<Char>) GetDirectoryName(ReadOnlySpan<Char>)

GetDirectoryName(String) GetDirectoryName(String) GetDirectoryName(String) GetDirectoryName(String)

Returns the directory information for the specified path string.

public:
 static System::String ^ GetDirectoryName(System::String ^ path);
public static string GetDirectoryName (string path);
static member GetDirectoryName : string -> string
Public Shared Function GetDirectoryName (path As String) As String

Parameters

path
String String String String

The path of a file or directory.

Returns

Directory information for path, or null if path denotes a root directory or is null. Returns Empty if path does not contain directory information.

Exceptions

The path parameter contains invalid characters, is empty, or contains only white spaces.

In the .NET for Windows Store apps or the Portable Class Library, catch the base class exception, IOException, instead.

The path parameter is longer than the system-defined maximum length.

Examples

The following code example demonstrates using the GetDirectoryName method on a Windows-based desktop platform.

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 ''
*/
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 ''
*/
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 ''

Remarks

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.

See Also

GetDirectoryName(ReadOnlySpan<Char>) GetDirectoryName(ReadOnlySpan<Char>) GetDirectoryName(ReadOnlySpan<Char>) GetDirectoryName(ReadOnlySpan<Char>)

public:
 static ReadOnlySpan<char> GetDirectoryName(ReadOnlySpan<char> path);
public static ReadOnlySpan<char> GetDirectoryName (ReadOnlySpan<char> path);
static member GetDirectoryName : ReadOnlySpan<char> -> ReadOnlySpan<char>
Public Shared Function GetDirectoryName (path As ReadOnlySpan(Of Char)) As ReadOnlySpan(Of Char)

Parameters

Returns

Applies to