This documentation is archived and is not being maintained.

DirectoryInfo Class

Exposes instance methods for creating, moving, and enumerating through directories and subdirectories. This class cannot be inherited.

Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)

<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public NotInheritable Class DirectoryInfo
	Inherits FileSystemInfo
Dim instance As DirectoryInfo

/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class DirectoryInfo extends FileSystemInfo
public final class DirectoryInfo extends FileSystemInfo
Not applicable.

Use the DirectoryInfo class for typical operations such as copying, moving, renaming, creating, and deleting directories.

If you are going to reuse an object several times, consider using the instance method of DirectoryInfo instead of the corresponding static methods of the Directory class, because a security check will not always be necessary.


In members that accept a path as an input string, that path must be well-formed or an exception is raised. For example, if a path is fully qualified but begins with a space, the path is not trimmed in methods of the class. Therefore, the path is malformed and an exception is raised. Similarly, a path or a combination of paths cannot be fully qualified twice. For example, "c:\temp c:\windows" also raises an exception in most cases. Ensure that your paths are well-formed when using methods that accept a path string.

In members that accept a path, the path can refer to a file or just a directory. The specified path can also refer to a relative path or a Universal Naming Convention (UNC) path for a server and share name. For example, all the following are acceptable paths:

  • "c:\\MyDir\\MyFile.txt" in C#, or "c:\MyDir\MyFile.txt" in Visual Basic.

  • "c:\\MyDir" in C#, or "c:\MyDir" in Visual Basic.

  • "MyDir\\MySubdir" in C#, or "MyDir\MySubDir" in Visual Basic.

  • "\\\\MyServer\\MyShare" in C#, or "\\MyServer\MyShare" in Visual Basic.

By default, full read/write access to new directories is granted to all users.

For a list of common I/O tasks, see Common I/O Tasks.

Windows Mobile 2003 for Pocket PC, Windows Mobile 2003 for Smartphone, Windows CE Platform Note: Because file systems for devices operate differently, the .NET Compact Framework does not support getting or setting directory attributes.

The following example demonstrates some of the main members of the DirectoryInfo class.

Imports System
Imports System.IO

Public Class Test
    Public Shared Sub Main()
        ' Specify the directories you want to manipulate.
        Dim di As DirectoryInfo = New DirectoryInfo("c:\MyDir")
            ' Determine whether the directory exists.
            If di.Exists Then
                ' Indicate that it already exists.
                Console.WriteLine("That path exists already.")
            End If

            ' Try to create the directory.
            Console.WriteLine("The directory was created successfully.")

            ' Delete the directory.
            Console.WriteLine("The directory was deleted successfully.")

        Catch e As Exception
            Console.WriteLine("The process failed: {0}", e.ToString())
        End Try
    End Sub
End Class

import System.*;
import System.IO.*;

class Test
    public static void main(String[] args)
        // Specify the directories you want to manipulate.
        DirectoryInfo di = new DirectoryInfo("c:\\MyDir");

        try {
            // Determine whether the directory exists.
            if (di.get_Exists()) {
                // Indicate that the directory already exists.
                Console.WriteLine("That path exists already.");

            // Try to create the directory.
            Console.WriteLine("The directory was created successfully.");

            // Delete the directory.
            Console.WriteLine("The directory was deleted successfully.");
        catch (System.Exception e) {
            Console.WriteLine("The process failed: {0}", e.ToString());
        finally {
    } //main
} //Test

The following example demonstrates how to copy a directory and its contents.

Imports System
Imports System.IO

Class CopyDir
    Shared Sub Copy(ByVal sourceDirectory As String, ByVal targetDirectory As String)
        Dim diSource As DirectoryInfo = New DirectoryInfo(sourceDirectory)
        Dim diTarget As DirectoryInfo = New DirectoryInfo(targetDirectory)

        CopyAll(diSource, diTarget)
    End Sub

    Shared Sub CopyAll(ByVal source As DirectoryInfo, ByVal target As DirectoryInfo)

        ' Check if the target directory exists, if not, create it.
        If Directory.Exists(target.FullName) = False Then
        End If

        ' Copy each file into it's new directory.
        For Each fi As FileInfo In source.GetFiles()
            Console.WriteLine("Copying {0}\{1}", target.FullName, fi.Name)
            fi.CopyTo(Path.Combine(target.ToString(), fi.Name), True)

        ' Copy each subdirectory using recursion.
        For Each diSourceSubDir As DirectoryInfo In source.GetDirectories()
            Dim nextTargetSubDir As DirectoryInfo = target.CreateSubdirectory(diSourceSubDir.Name)
            CopyAll(diSourceSubDir, nextTargetSubDir)
    End Sub

    Shared Sub Main()
        Dim sourceDirectory As String = "c:\\sourceDirectory"
        Dim targetDirectory As String = "c:\\targetDirectory"

        Copy(sourceDirectory, targetDirectory)
    End Sub
    ' Output will vary based on the contents of the source directory.
End Class

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0