Export (0) Print
Expand All

Path.GetDirectoryName Method

Returns the directory information for the specified path string.

Namespace:  System.IO
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
Public Shared Function GetDirectoryName ( _
	path As String _
) As String

Parameters

path
Type: System.String

The path of a file or directory.

Return Value

Type: System.String
Directory information for path, or Nothing if path denotes a root directory or is null. Returns String.Empty if path does not contain directory information.

ExceptionCondition
ArgumentException

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

PathTooLongException
NoteNote

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.

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

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft