DirectoryInfo.Parent プロパティ

定義

指定したサブディレクトリの親ディレクトリを取得します。

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 の場合、またはファイル パスがルート (\C:\\\server\shareなど) を表す場合にnullします。

例外

呼び出し元に必要なアクセス許可がありません。

次の例では、指定したディレクトリの親ディレクトリを参照しています。

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

注釈

Important

.NET Framework では、Parent は相対パスを返します。 .NET Core では、Parent は完全修飾パスを返します。

バージョン間で一貫した動作を保証し、意図を明示的にするには、Parentによって返されるDirectoryInfo インスタンスで次のいずれかのプロパティの値を取得します。

  • Nameは、ディレクトリの単純な名前 ( bin など) を返します。
  • FullNameは、ディレクトリの絶対パスを返します。

一般的な I/O タスクの一覧については、「 一般的な I/O タスク」を参照してください。

適用対象

こちらもご覧ください