Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

DirectoryInfo.Parent Property

Gets the parent directory of a specified subdirectory.

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

Property Value

The parent directory, or a null reference (Nothing in Visual Basic) if the path is null or if the file path denotes a root (such as "\", "C:", or * "\\server\share").

Exceptions

Exception Type Condition
SecurityException The caller does not have the required permission.

Remarks

For an example of using this method, see the Example section below. The following table lists examples of other typical or related I/O tasks.

To do this... See the example in this topic...
Copy a directory. Directory
Rename or move a directory. Directory.Move

DirectoryInfo.MoveTo

Delete a directory. Directory.Delete

DirectoryInfo.Delete

Create a directory. CreateDirectory

Directory

Create a subdirectory. CreateSubdirectory
See the files in a directory. Name
See the subdirectories of a directory. GetDirectories

GetDirectories

See all the files in all subdirectories of a directory. GetFileSystemInfos
Find the size of a directory. Directory
Determine if a file exists. Exists
Sort files in a directory by size. GetFileSystemInfos
Determine if a directory exists. Exists

Example

The following example demonstrates referring to the parent directory of a specified directory.

[Visual Basic] 
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();

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

.NET Framework Security: 

See Also

DirectoryInfo Class | DirectoryInfo Members | System.IO Namespace | Working with I/O | Reading Text from a File | Writing Text to a File

Show:
© 2015 Microsoft