DirectoryInfo.Parent 属性

定义

获取指定的子目录的父目录。

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

属性值

父目录,或者 null 如果路径为 null,或者如果文件路径表示根 ((如 \C:\\\server\share) )。

例外

调用方没有所要求的权限。

示例

以下示例引用指定目录的父目录。

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

注解

重要

在 .NET Framework 中, Parent 返回相对路径。 在 .NET Core 中, Parent 返回完全限定的路径。

若要确保各版本的行为一致并使意向明确,请在 返回Parent的 实例上DirectoryInfo检索以下属性之一的值。

  • Name,返回目录 (的简单名称,例如 bin) 。
  • FullName,返回目录的绝对路径。

有关常见 I/O 任务的列表,请参阅 常见 I/O 任务

适用于

另请参阅