Path.GetDirectoryName Metoda

Definicja

Przeciążenia

GetDirectoryName(String)

Zwraca informacje o katalogu dla określonej ścieżki.

GetDirectoryName(ReadOnlySpan<Char>)

Zwraca informacje o katalogu dla określonej ścieżki reprezentowanej przez zakres znaków.

GetDirectoryName(String)

Źródło:
Path.cs
Źródło:
Path.cs
Źródło:
Path.cs

Zwraca informacje o katalogu dla określonej ścieżki.

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

Parametry

path
String

Ścieżka pliku lub katalogu.

Zwraca

Informacje o katalogu dla pathelementu lub null , jeśli path oznacza katalog główny lub ma wartość null. Zwraca wartość Empty , jeśli path nie zawiera informacji o katalogu.

Wyjątki

.NET Framework i .NET Core w wersjach starszych niż 2.1: path Parametr zawiera nieprawidłowe znaki, jest pusty lub zawiera tylko białe spacje.

Parametr path jest dłuższy niż długość maksymalna zdefiniowana przez system.

Uwaga: na platformie .NET dla aplikacji ze Sklepu Windows lub przenośnej biblioteki klas przechwyć wyjątek klasy bazowej , IOExceptionzamiast tego.

Przykłady

W poniższym przykładzie pokazano użycie GetDirectoryName metody na platformie klasycznej opartej na systemie Windows.

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

Uwagi

W większości przypadków ciąg zwracany przez tę metodę składa się ze wszystkich znaków w ścieżce do, ale nie obejmuje znaków separatora ostatniego katalogu. Znak separatora katalogu może mieć wartość DirectorySeparatorChar lub AltDirectorySeparatorChar. Jeśli ścieżka składa się z katalogu głównego, takiego jak "c:\", null jest zwracana.

Ta metoda nie obsługuje ścieżek przy użyciu ciągu "file:".

Ponieważ zwrócona ścieżka nie zawiera znaków separatora ostatniego katalogu, przekazanie zwróconej ścieżki z powrotem do GetDirectoryName metody obcina jeden poziom folderu na kolejne wywołanie ścieżki wyników. Na przykład przekazanie ścieżki "C:\Directory\SubDirectory\test.txt" do GetDirectoryName zwraca wartości "C:\Directory\SubDirectory". Przekazywanie tej ścieżki "C:\Directory\SubDirectory" do GetDirectoryName zwraca "C:\Directory".

Aby uzyskać listę typowych zadań we/wy, zobacz Typowe zadania we/wy.

Zobacz też

Dotyczy

GetDirectoryName(ReadOnlySpan<Char>)

Źródło:
Path.cs
Źródło:
Path.cs
Źródło:
Path.cs

Zwraca informacje o katalogu dla określonej ścieżki reprezentowanej przez zakres znaków.

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)

Parametry

path
ReadOnlySpan<Char>

Ścieżka pobierania informacji o katalogu z.

Zwraca

Informacje o katalogu dla pathelementu lub pustego zakresu, jeśli path to null, pusty zakres lub katalog główny (taki jak \, C:lub \\server\share).

Uwagi

W przeciwieństwie do przeciążenia ciągu ta metoda nie normalizuje separatorów katalogów.

Zobacz też

Dotyczy