Język

DirectoryInfo.Parent Właściwość

Definicja

Pobiera katalog nadrzędny określonego podkatalogu.

public:
 property System::IO::DirectoryInfo ^ Parent { System::IO::DirectoryInfo ^ get(); };
public System.IO.DirectoryInfo Parent { get; }
public System.IO.DirectoryInfo? Parent { get; }
member this.Parent : System.IO.DirectoryInfo
Public ReadOnly Property Parent As DirectoryInfo

Wartość właściwości

Katalog nadrzędny lub null ścieżka ma wartość null lub jeśli ścieżka pliku oznacza katalog główny (taki jak \, C:\lub \\server\share).

Wyjątki

Obiekt wywołujący nie ma wymaganych uprawnień.

Przykłady

W poniższym przykładzie odwołuje się do katalogu nadrzędnego określonego katalogu.

using System;
using System.IO;

public class MoveToTest
{
    public static void Main()
    {

        // Make a reference to a directory.
        DirectoryInfo di = new DirectoryInfo("TempDir");

        // Create the directory only if it does not already exist.
        if (!di.Exists)
            di.Create();

        // Create a subdirectory in the directory just created.
        DirectoryInfo dis = di.CreateSubdirectory("SubDir");

        // Get a reference to the parent directory of the subdirectory you just made.
        DirectoryInfo parentDir = dis.Parent;
        Console.WriteLine("The parent directory of '{0}' is '{1}'", dis.Name, parentDir.Name);

        // Delete the parent directory.
        di.Delete(true);
    }
}
open System.IO

// Make a reference to a directory.
let di = DirectoryInfo "TempDir"

// Create the directory only if it does not already exist.
if not di.Exists then
    di.Create()

// Create a subdirectory in the directory just created.
let dis = di.CreateSubdirectory "SubDir"

// Get a reference to the parent directory of the subdirectory you just made.
let parentDir = dis.Parent
printfn $"The parent directory of '{dis.Name}' is '{parentDir.Name}'"

// Delete the parent directory.
di.Delete true
Imports System.IO

Public Class MoveToTest

    Public Shared Sub Main()
        ' Make a reference to a directory.
        Dim di As New DirectoryInfo("TempDir")
        ' Create the directory only if it does not already exist.
        If di.Exists = False Then
            di.Create()
        End If

        ' Create a subdirectory in the directory just created.
        Dim dis As DirectoryInfo = di.CreateSubdirectory("SubDir")

        ' Get a reference to the parent directory of the subdirectory you just made.
        Dim parentDir As DirectoryInfo = dis.Parent
        Console.WriteLine("The parent directory of '{0}' is '{1}'", dis.Name, parentDir.Name)

        ' Delete the parent directory.
        di.Delete(True)
    End Sub
End Class

Uwagi

Ważna

W .NET Framework Parent zwraca ścieżkę względną. W .NET Core Parent zwraca w pełni kwalifikowaną ścieżkę.

Aby zapewnić spójne zachowanie w różnych wersjach i jawnie ustawić intencję, pobierz wartość jednej z następujących właściwości wystąpienia DirectoryInfo zwróconego przez Parentelement .

  • Name, który zwraca prostą nazwę katalogu (na przykład bin).
  • FullName, który zwraca ścieżkę bezwzględną katalogu.

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

Dotyczy

Zobacz też