Path.GetDirectoryName Metoda

Definice

Přetížení

GetDirectoryName(String)

Vrátí informace o adresáři pro zadanou cestu.

GetDirectoryName(ReadOnlySpan<Char>)

Vrátí informace o adresáři pro zadanou cestu reprezentovanou rozsahem znaků.

GetDirectoryName(String)

Zdroj:
Path.cs
Zdroj:
Path.cs
Zdroj:
Path.cs

Vrátí informace o adresáři pro zadanou cestu.

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

Cesta k souboru nebo adresáři.

Návraty

Informace o adresáři pro path, nebo null pokud path označuje kořenový adresář nebo má hodnotu null. Vrátí, Empty pokud path neobsahuje informace o adresáři.

Výjimky

.NET Framework a .NET Core verze starší než 2.1: Parametr path obsahuje neplatné znaky, je prázdný nebo obsahuje pouze prázdné znaky.

Parametr path je delší než maximální délka definovaná systémem.

Poznámka: V .NET pro aplikace pro Windows Store nebo v přenosné knihovně tříd místo toho zachyťte výjimku IOExceptionzákladní třídy.

Příklady

Následující příklad ukazuje použití GetDirectoryName metody na desktopové platformě se systémem 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 ''

Poznámky

Ve většině případů se řetězec vrácený touto metodou skládá ze všech znaků v cestě až k posledním znakům oddělovače adresáře, ale nezahrnout je. Znak oddělovače adresáře může být buď DirectorySeparatorChar nebo AltDirectorySeparatorChar. Pokud cesta sestává z kořenového adresáře, například "c:\", null je vrácena.

Tato metoda nepodporuje cesty pomocí příkazu "file:".

Vzhledem k tomu, že vrácená cesta neobsahuje poslední znaky oddělovače adresáře, předání vrácené cesty zpět do GetDirectoryName metody zkrátí jednu úroveň složky na následné volání výsledné cesty. Například předání cesty "C:\Directory\SubDirectory\test.txt" do GetDirectoryName vrátí "C:\Directory\Podadresář". Předání této cesty C:\Directory\Podadresáře do GetDirectoryName vrátí hodnotu C:\Directory.

Seznam běžných vstupně-výstupních úloh najdete v tématu Běžné vstupně-výstupní úlohy.

Viz také

Platí pro

GetDirectoryName(ReadOnlySpan<Char>)

Zdroj:
Path.cs
Zdroj:
Path.cs
Zdroj:
Path.cs

Vrátí informace o adresáři pro zadanou cestu reprezentovanou rozsahem znaků.

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>

Cesta, ze které chcete načíst informace o adresáři.

Návraty

Informace o adresáři pro path, nebo prázdné rozpětí, pokud path je null, prázdné rozpětí nebo kořen (například \, C:, nebo \\server\share).

Poznámky

Na rozdíl od přetížení řetězců tato metoda nenormalizuje oddělovače adresářů.

Viz také

Platí pro