.NET Framework Class Library
Path..::.GetDirectoryName Method

Returns the directory information for the specified path string.

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

Visual Basic (Declaration)
Public Shared Function GetDirectoryName ( _
    path As String _
) As String
Visual Basic (Usage)
Dim path As String
Dim returnValue As String

returnValue = Path.GetDirectoryName(path)
C#
public static string GetDirectoryName(
    string path
)
Visual C++
public:
static String^ GetDirectoryName(
    String^ path
)
JScript
public static function GetDirectoryName(
    path : String
) : String

Parameters

path
Type: System..::.String
The path of a file or directory.

Return Value

Type: System..::.String
A String containing directory information for path, or nullNothingnullptra null reference (Nothing in Visual Basic) if path denotes a root directory, is the empty string (""), or is nullNothingnullptra null reference (Nothing in Visual Basic). Returns String..::.Empty if path does not contain directory information.
Exceptions

ExceptionCondition
ArgumentException

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

PathTooLongException

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

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.

Examples

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

Visual Basic
Dim fileName As String = "C:\mydir\myfile.ext"
Dim pathname As String = "C:\mydir\"
Dim rootPath As String = "C:\"
Dim directoryName As String

directoryName = Path.GetDirectoryName(fileName)
Console.WriteLine("GetDirectoryName('{0}') returns '{1}'", fileName, directoryName)

directoryName = Path.GetDirectoryName(pathname)
Console.WriteLine("GetDirectoryName('{0}') returns '{1}'", pathname, 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 ''
C#
string fileName = @"C:\mydir\myfile.ext";
string path = @"C:\mydir\";
string rootPath = @"C:\";
string directoryName;

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

*/
Visual C++
String^ fileName = "C:\\mydir\\myfile.ext";
String^ path = "C:\\mydir\\";
String^ rootPath = "C:\\";
String^ directoryName;
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 ''

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

*/
Platforms

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

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

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

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
See Also

Reference

Other Resources

Tags :


Community Content

Dave Sexton
Conflicting Documentation for Empty String Handling
The Return Value states that null is returned if path is the empty string (""), but that's not correct. In practice, an ArgumentException is thrown as indicated in the Exceptions section.
Tags : contentbug

LuckyLindy
Coding error in function
This function will return an invalid PathTooLongException if you send it a path / filename combination that is longer than 260 characters. It should handle this since the path is less than 248 characters (it is 240) and the filename is 27.

To use, remove the file name from the full path before sending to function:

System.IO.Path.GetDirectoryName(strFullFilePath.Replace(strFileName,

""))

Test Case:
"C:\XXXXXX XXXXXX\XXXXXXX\XXXXXXXXXXXXXXXXX\XX XXX XXXX XXX XXXXX\XXXXXXXXXXXX_XX_XXXXXXXX-X\XXXXXXXXX\XXXXXXXXXXXXXXXX\XXXXXXXX\XXXXX-XXXXXX\XXXXXXXXXX XXXXXXX XXXXXX XXXXX XX-XXX-XXXX\XXXXXXXXX XXXXXXXXXX XXXXX XX XXXXXX XXXXXX XXX X.XXXX\XXXXXXXX XXX XXXX XXXX.ZIP"

.NET version 2.0
Windows XP Professional

Page view tracker