DirectoryInfo.Parent DirectoryInfo.Parent DirectoryInfo.Parent DirectoryInfo.Parent Property

定義

取得指定子目錄的父代 (Parent) 目錄。Gets the parent directory of a specified subdirectory.

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

屬性值

父目錄, null如果路徑是 null 或檔案路徑代表根目錄( \例如、 C:\\\server\share),則為。The parent directory, or null if the path is null or if the file path denotes a root (such as \, C:\, or \\server\share).

例外狀況

呼叫端沒有必要的使用權限。The caller does not have the required permission.

範例

下列範例會參考指定目錄的父目錄。The following example refers to the parent directory of a specified directory.

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);
    }
}
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 'MoveToTest

備註

重要

在 .NET Framework 中Parent ,會傳回相對路徑。In .NET Framework, Parent returns a relative path. 在 .net Core 中Parent ,會傳回完整路徑。In .NET Core, Parent returns a fully qualified path.

若要確保各個版本之間的一致行為,並讓您的意圖明確,請在所DirectoryInfo Parent傳回的實例上取出下列其中一個屬性的值。To ensure consistent behavior across versions and to make your intent explicit, retrieve the value of one of the following properties on the DirectoryInfo instance returned by Parent.

  • Name,它會傳回目錄的簡單名稱(例如bin)。Name, which returns the simple name of the directory (such as bin).
  • FullName,它會傳回目錄的絕對路徑。FullName, which returns the absolute path of the directory.

如需一般 i/o 工作的清單, 請參閱一般 i/o工作。For a list of common I/O tasks, see Common I/O Tasks.

安全性

FileIOPermission
用於讀取檔案和目錄,以及存取路徑。for reading from files and directories and for access to the path. 相關聯的FileIOPermissionAccess列舉:、FileIOPermissionAccessAssociated enumerations: FileIOPermissionAccess, FileIOPermissionAccess

適用於

另請參閱