Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Path.GetDirectoryName Method

Returns the directory information for the specified path string.

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

public static string GetDirectoryName (
	string path
)
public static String GetDirectoryName (
	String path
)
public static function GetDirectoryName (
	path : String
) : String

Parameters

path

The path of a file or directory.

Return Value

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

Exception typeCondition

ArgumentException

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

PathTooLongException

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 an example of using this method, see the Example section. The following table lists examples of other typical or related I/O tasks.

To do this...

See the example in this topic...

Create a text file.

How to: Write Text to a File

Write to a text file.

How to: Write Text to a File

Read from a text file.

How to: Read Text from a File

Retrieve a file extension.

GetExtension

Retrieve the fully qualified path of a file.

GetFullPath

Retrieve only the file name from a path.

GetFileNameWithoutExtension

Change the extension of a file.

ChangeExtension

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

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

*/

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

*/

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 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

Show:
© 2014 Microsoft