संपादित करें

इसके माध्यम से साझा किया गया


Path.GetDirectoryName Method

Definition

Overloads

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.

GetDirectoryName(String)

Source:
Path.cs
Source:
Path.cs
Source:
Path.cs

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

Parameters

path
String

The path of a file or directory.

Returns

Directory information for path, or null if path denotes a root directory or is null. Returns Empty if path does not contain directory information.

Exceptions

.NET Framework and .NET Core versions older than 2.1: The path parameter contains invalid characters, is empty, or contains only white spaces.

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

Note: In .NET for Windows Store apps or the Portable Class Library, catch the base class exception, IOException, instead.

Examples

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

Remarks

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). A directory separator character can be either DirectorySeparatorChar or AltDirectorySeparatorChar. If the path consists of a root directory, such as "c:\", null is returned.

This method does not support paths using "file:".

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. For example, passing the path "C:\Directory\SubDirectory\test.txt" into GetDirectoryName returns "C:\Directory\SubDirectory". Passing that path, "C:\Directory\SubDirectory", into GetDirectoryName returns "C:\Directory".

For a list of common I/O tasks, see Common I/O tasks.

See also

Applies to

GetDirectoryName(ReadOnlySpan<Char>)

Source:
Path.cs
Source:
Path.cs
Source:
Path.cs

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)

Parameters

path
ReadOnlySpan<Char>

The path to retrieve the directory information from.

Returns

Directory information for path, or an empty span if path is null, an empty span, or a root (such as \, C:, or \\server\share).

Remarks

Unlike the string overload, this method doesn't normalize directory separators.

See also

Applies to