Bagikan melalui


DirectoryInfo.Parent Properti

Definisi

Mendapatkan direktori induk dari subdirektori tertentu.

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

Nilai Properti

Direktori induk, atau null jika jalur null atau jika jalur file menunjukkan akar (seperti \, , C:\atau \\server\share).

Pengecualian

Pemanggil tidak memiliki izin yang diperlukan.

Contoh

Contoh berikut mengacu pada direktori induk dari direktori tertentu.

using namespace System;
using namespace System::IO;
int main()
{
   
   // Make a reference to a directory.
   DirectoryInfo^ di = gcnew 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 );
}
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 == false)
            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

Keterangan

Penting

Dalam .NET Framework, Parent mengembalikan jalur relatif. Di .NET Core, Parent mengembalikan jalur yang sepenuhnya memenuhi syarat.

Untuk memastikan perilaku yang konsisten di seluruh versi dan untuk membuat niat Anda eksplisit, ambil nilai salah satu properti berikut pada instans yang DirectoryInfo dikembalikan oleh Parent.

  • Name, yang mengembalikan nama sederhana direktori (seperti bin).
  • FullName, yang mengembalikan jalur absolut direktori.

Untuk daftar tugas I/O umum, lihat Tugas I/O Umum.

Berlaku untuk

Lihat juga