Export (0) Print
Expand All

FileInfo.Directory Property

Gets an instance of the parent directory.

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

Property Value

A DirectoryInfo object representing the parent directory of this file.

Exceptions

Exception Type Condition
DirectoryNotFoundException The specified path is invalid, such as being on an unmapped drive.
SecurityException The caller does not have the required permission.

Remarks

To get the parent directory as a string, use the DirectoryName property.

For an example of using this property, 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
Determine if a directory exists. Exists

Example

The following example opens or creates a file, determines its full path, and determines and displays the full contents of the directory.

[Visual Basic] 
Imports System
Imports System.IO

Public Class DirectoryTest

    Public Shared Sub Main()
        ' Open an existing file, or create a new one.
        Dim fi As New FileInfo("temp.txt")

        ' Determine the full path of the file just created.
        Dim di As DirectoryInfo = fi.Directory

        ' Figure out what other entries are in that directory.
        Dim fsi As FileSystemInfo() = di.GetFileSystemInfos()

        ' Print the names of all the files and subdirectories of that directory.
        Console.WriteLine("The directory '{0}' contains the following files and directories:", di.FullName)
        Dim info As FileSystemInfo
        For Each info In fsi
            Console.WriteLine(info.Name)
        Next info
    End Sub 'Main
End Class 'DirectoryTest

[C#] 
using System;
using System.IO;

public class DirectoryTest 
{
    public static void Main() 
    {

        // Open an existing file, or create a new one.
        FileInfo fi = new FileInfo("temp.txt");

        // Determine the full path of the file just created.
        DirectoryInfo di = fi.Directory;

        // Figure out what other entries are in that directory.
        FileSystemInfo[] fsi = di.GetFileSystemInfos();

        Console.WriteLine("The directory '{0}' contains the following files and directories:", di.FullName);

        // Print the names of all the files and subdirectories of that directory.
        foreach (FileSystemInfo info in fsi)
            Console.WriteLine(info.Name);
    }
}

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

using namespace System;
using namespace System::IO;

int main() {

    // Open an existing file, or create a new one.
    FileInfo* fi = new FileInfo(S"temp.txt");

    // Determine the full path of the file just created.
    DirectoryInfo* di = fi->Directory;

    // Figure out what other entries are in that directory.
    FileSystemInfo* fsi[] = di->GetFileSystemInfos();

    Console::WriteLine(S"The directory '{0}' contains the following files and directories:", di->FullName);

    // Print the names of all the files and subdirectories of that directory.
    Collections::IEnumerator* myEnum = fsi->GetEnumerator();
    while (myEnum->MoveNext()) {
        FileSystemInfo* info = __try_cast<FileSystemInfo*>(myEnum->Current);
        Console::WriteLine(info->Name);
    }
}

[JScript] 
import System;
import System.IO;

public class DirectoryTest {
    public static function Main() : void {

        // Open an existing file, or create a new one.
        var fi : FileInfo = new FileInfo("temp.txt");

        // Determine the full path of the file just created.
        var di : DirectoryInfo = fi.Directory;

        // Figure out what other entries are in that directory.
        var fsi : FileSystemInfo[] = di.GetFileSystemInfos();

        Console.WriteLine("The directory '{0}' contains the following files and directories:", di.FullName);

        // Print the names of all the files and subdirectories of that directory.
        for (var i : int in fsi)
            Console.WriteLine(fsi[i].Name);
    }
}
DirectoryTest.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

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

Show:
© 2014 Microsoft