次の方法で共有


DirectoryInfo.Parent プロパティ

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

Public ReadOnly Property Parent As DirectoryInfo
[C#]
public DirectoryInfo Parent {get;}
[C++]
public: __property DirectoryInfo* get_Parent();
[JScript]
public function get Parent() : DirectoryInfo;

プロパティ値

親ディレクトリ。または、パスが null であるか、ファイル パスがルート ("\"、"C:"、* "\\server\share" など) を示している場合は null 参照 (Visual Basic では Nothing) 。

例外

例外の種類 条件
SecurityException 呼び出し元に、必要なアクセス許可がありません。

解説

このメソッドの使用例については、以下の「使用例」を参照してください。その他の一般的な I/O タスクまたは関連する I/O タスクの例を次の表に示します。

実行するタスク 参考例があるトピック
ディレクトリをコピーする。 Directory
ディレクトリの名前を変更、またはディレクトリを移動する。 Directory.Move

DirectoryInfo.MoveTo

ディレクトリを削除する。 Directory.Delete

DirectoryInfo.Delete

ディレクトリを作成する。 CreateDirectory

Directory

サブディレクトリを作成する。 CreateSubdirectory
ディレクトリ内のファイルを参照する。 Name
ディレクトリ内のサブディレクトリを参照する。 GetDirectories

GetDirectories

ディレクトリ内のすべてのサブディレクトリにあるすべてのファイルを参照する。 GetFileSystemInfos
ディレクトリのサイズを取得する。 Directory
ファイルが存在するかどうかを判別する。 Exists
ディレクトリ内のファイルをサイズ順に並べ替える。 GetFileSystemInfos
ディレクトリが存在するかどうかを判別する。 Exists

使用例

指定したディレクトリの親ディレクトリを参照する例を次に示します。

 
Imports System
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 'Main
End Class 'MoveToTest

[C#] 
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);
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::IO;

int main() {

    // Make a reference to a directory.
    DirectoryInfo* di = new DirectoryInfo(S"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(S"SubDir");

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

    // Delete the parent directory.
    di->Delete(true);
}

[JScript] 
import System;
import System.IO;

public class MoveToTest {
    public static function Main() {

        // Make a reference to a directory.
        var di : DirectoryInfo = 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.
        var dis : DirectoryInfo = di.CreateSubdirectory("SubDir");

        // Get a reference to the parent directory of the subdirectory you just made.
        var parentDir : DirectoryInfo = dis.Parent;

        Console.WriteLine("The parent directory of '{0}' is '{1}'", dis.Name, parentDir.Name);

        // Delete the parent directory.
        di.Delete(true);
    }
}
MoveToTest.Main();

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

.NET Framework セキュリティ:

参照

DirectoryInfo クラス | DirectoryInfo メンバ | System.IO 名前空間 | 入出力操作 | ファイルからのテキストの読み取り | ファイルへのテキストの書き込み