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 pathnebo null , pokud path označuje kořenový adresář nebo má hodnotu null. Vrátí hodnotu Empty , pokud path neobsahuje informace o adresáři.

Výjimky

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

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

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

Příklady

Následující příklad ukazuje použití metody na GetDirectoryName 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 ne včetně). Znak oddělovače adresářů může být nebo DirectorySeparatorCharAltDirectorySeparatorChar. Pokud cesta obsahuje kořenový adresář, například c:\, null vrátí se.

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

Vzhledem k tomu, že vrácená cesta neobsahuje poslední znaky oddělovače adresářů, předání vrácené cesty zpět do GetDirectoryName metody zkrátí jednu úroveň složky při každém následném volání na výsledné cestě. Například předání cesty "C:\Directory\SubDirectory\test.txt" do GetDirectoryName vrátí "C:\Directory\Podadresář". Předání cesty "C:\Directory\Podadresář" do GetDirectoryName vrátí "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é se mají načíst informace o adresáři.

Návraty

Informace o adresáři pro pathnebo prázdný rozsah, pokud path je null, prázdný rozsah 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