Path.GetDirectoryName 方法

定义

重载

GetDirectoryName(ReadOnlySpan<Char>)

返回由字符范围表示的指定路径的目录信息。Returns the directory information for the specified path represented by a character span.

GetDirectoryName(String)

返回指定路径的目录信息。Returns the directory information for the specified path.

GetDirectoryName(ReadOnlySpan<Char>)

返回由字符范围表示的指定路径的目录信息。Returns the directory information for the specified path represented by a character span.

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)

参数

path
ReadOnlySpan<Char>

要从中检索目录信息的路径。The path to retrieve the directory information from.

返回

ReadOnlySpan<Char>

path 的目录信息,如果 pathnull空范围或根(例如 ,C: 或\server\share),则为空跨度。Directory information for path, or an empty span if path is null, an empty span, or a root (such as , C:, or \server\share).

注解

与字符串重载不同,此方法不会将目录分隔符标准化。Unlike the string overload, this method doesn't normalize directory separators.

适用于

GetDirectoryName(String)

返回指定路径的目录信息。Returns the directory information for the specified path.

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

参数

path
String

文件或目录的路径。The path of a file or directory.

返回

String

path 的目录信息;如果 path 表示根目录或为 null,则为 nullDirectory information for path, or null if path denotes a root directory or is null. 如果 path 不包含目录信息,则返回 EmptyReturns Empty if path does not contain directory information.

例外

.NET Framework 和 .NET Core 版本早于2.1: path 参数包含无效字符、为空或仅包含空格。.NET Framework and .NET Core versions older than 2.1: The path parameter contains invalid characters, is empty, or contains only white spaces.

path 参数超过系统定义的最大长度。The path parameter is longer than the system-defined maximum length.

示例

下面的示例演示如何 GetDirectoryName 在基于 Windows 的桌面平台上使用方法。The following 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 Then
       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 ''

注解

在大多数情况下,此方法返回的字符串包含路径中的所有字符,但不包括最后一个目录分隔符 (s) 。In most cases, the string returned by this method consists of all characters in the path up to, but not including, the last directory separator character(s). 目录分隔符可以是 DirectorySeparatorCharAltDirectorySeparatorCharA directory separator character can be either DirectorySeparatorChar or AltDirectorySeparatorChar. 如果路径包含根目录(如 "c: \ "), null 则返回。If the path consists of a root directory, such as "c:\", null is returned.

此方法不支持使用 "file:" 的路径。This method does not support paths using "file:".

由于返回的路径不包括) 的最后一个目录 (分隔符,因此将返回的路径传递回方法会 GetDirectoryName 在对结果路径的每个后续调用中截断一个文件夹级别。Because the returned path does not include the last directory separator character(s), passing the returned path back into the GetDirectoryName method truncates one folder level per subsequent call on the result path. 例如,将路径 "C:\Directory\SubDirectory\test.txt" 传递到将 GetDirectoryName 返回 "C:\Directory\SubDirectory"。For example, passing the path "C:\Directory\SubDirectory\test.txt" into GetDirectoryName returns "C:\Directory\SubDirectory". 将该路径 "C:\Directory\SubDirectory" 传递到将 GetDirectoryName 返回 "C:\Directory"。Passing that path, "C:\Directory\SubDirectory", into GetDirectoryName returns "C:\Directory".

有关常见 i/o 任务的列表,请参阅 常见 i/o 任务For a list of common I/O tasks, see Common I/O tasks.

适用于