EN
Ce contenu n’est pas disponible dans votre langue. Voici la version anglaise.

Path.GetDirectoryName Method

Returns the directory information for the specified path string.

[Visual Basic]
Public Shared Function GetDirectoryName( _
   ByVal path As String _
) As String
[C#]
public static string GetDirectoryName(
 string path
);
[C++]
public: static String* GetDirectoryName(
 String* path
);
[JScript]
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). Returns String.Empty if path does not contain directory information.

Exceptions

Exception Type Condition
ArgumentException path contains invalid characters, is empty, or contains only white spaces.

Remarks

The string returned by this method consists of all characters in the path up to but excluding the last DirectorySeparatorChar or AltDirectorySeparatorChar character.

Calling this method with the path parameter set to "c:\" returns empty string. This method does not support paths with "file:".

For an example of using this method, see the Example section below. 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. Writing Text to a File
Write to a text file. Writing Text to a File
Read from a text file. Reading 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

Example

[Visual Basic, C#, C++] The following 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)

[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);

[C++] 
String * fileName = S"C:\\mydir\\myfile.ext";
String * path = S"C:\\mydir\\";
String * rootPath = S"C:\\";
String * directoryName;

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

directoryName = Path::GetDirectoryName(path);
Console::WriteLine(S"GetDirectoryName('{0}') returns '{1}'", 
   path, directoryName);

directoryName = Path::GetDirectoryName(rootPath);
Console::WriteLine(S"GetDirectoryName('{0}') returns '{1}'", 
   rootPath, directoryName);

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Path Class | Path Members | System.IO Namespace | Working with I/O | Reading Text from a File | Writing Text to a File

Afficher:
© 2014 Microsoft