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 string.

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.

返回

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 string.

public:
 static System::String ^ GetDirectoryName(System::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.

返回

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

异常

path 参数包含无效字符,为空,或仅包含空格。The path parameter contains invalid characters, is empty, or contains only white spaces.

适用于 Windows 应用商店应用的 .NET可移植类库中,改为捕获基类异常 IOExceptionIn the .NET for Windows Store apps or the Portable Class Library, catch the base class exception, IOException, instead.

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

示例

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

注解

在大多数情况下,此方法返回的字符串包含路径中的所有字符,但不包括最后的 DirectorySeparatorCharAltDirectorySeparatorCharIn 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. 如果路径包含根目录(如 "c:\"),则返回 null。If the path consists of a root directory, such as "c:\", null is returned. 请注意,此方法不支持使用 "file:" 的路径。Note that this method does not support paths using "file:". 由于返回的路径不包括 DirectorySeparatorCharAltDirectorySeparatorChar,因此将返回的路径传递回 GetDirectoryName 方法会导致对结果字符串的每个后续调用截断一个文件夹级别。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. 例如,将路径 "C:\Directory\SubDirectory\test.txt" 传递到 GetDirectoryName 方法将返回 "C:\Directory\SubDirectory"。For example, passing the path "C:\Directory\SubDirectory\test.txt" into the GetDirectoryName method will return "C:\Directory\SubDirectory". 将该字符串 "C:\Directory\SubDirectory" 传递到 GetDirectoryName 将导致 "C:\Directory"。Passing that string, "C:\Directory\SubDirectory", into GetDirectoryName will result in "C:\Directory".

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

另请参阅

适用于