Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

DirectoryInfo Class

Exposes instance methods for creating, moving, and enumerating through directories and subdirectories.

For a list of all members of this type, see DirectoryInfo Members.

System.Object
   System.MarshalByRefObject
      System.IO.FileSystemInfo
         System.IO.DirectoryInfo

[Visual Basic]
<Serializable>
NotInheritable Public Class DirectoryInfo
   Inherits FileSystemInfo
[C#]
[Serializable]
public sealed class DirectoryInfo : FileSystemInfo
[C++]
[Serializable]
public __gc __sealed class DirectoryInfo : public FileSystemInfo
[JScript]
public
   Serializable
class DirectoryInfo extends FileSystemInfo

Thread Safety

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

Remarks

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.

Note   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 an example of using this class, 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...
Create a text file. Writing Text to a File
Write to a text file. Writing Text to a File
Read from a text file. Reading Text from a File
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

.NET Compact Framework Platform Note:  Getting or setting directory attributes is not supported.

Example

[Visual Basic, C#, C++] The following example demonstrates some of the main members of the DirectoryInfo class.

[Visual Basic] 
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")
        Try
            ' Determine whether the directory exists.
            If di.Exists Then
                ' Indicate that it already exists.
                Console.WriteLine("That path exists already.")
                Return
            End If

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

            ' Delete the directory.
            di.Delete()
            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

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

class Test 
{
    public static void Main() 
    {
        // Specify the directories you want to manipulate.
        DirectoryInfo di = new DirectoryInfo(@"c:\MyDir");
        try 
        {
            // Determine whether the directory exists.
            if (di.Exists) 
            {
                // Indicate that the directory already exists.
                Console.WriteLine("That path exists already.");
                return;
            }

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

            // Delete the directory.
            di.Delete();
            Console.WriteLine("The directory was deleted successfully.");

        } 
        catch (Exception e) 
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        } 
        finally {}
    }
}

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

using namespace System;
using namespace System::IO;

int main() {
    // Specify the directories you want to manipulate.
    DirectoryInfo* di = new DirectoryInfo(S"c:\\MyDir");
    try {
        // Determine whether the directory exists.
        if (di->Exists) {
            // Indicate that the directory already exists.
            Console::WriteLine(S"That path exists already.");
            return 0;
        }

        // Try to create the directory.
        di->Create();
        Console::WriteLine(S"The directory was created successfully.");

        // Delete the directory.
        di->Delete();
        Console::WriteLine(S"The directory was deleted successfully.");
    } catch (Exception* e) {
        Console::WriteLine(S"The process failed: {0}", e);
    } 
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.IO

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

Assembly: Mscorlib (in Mscorlib.dll)

See Also

DirectoryInfo Members | System.IO Namespace | File | Attributes | Directory | Path | Working with I/O | Reading Text from a File | Writing Text to a File | Basic File I/O | Reading and Writing to a Newly Created Data File

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft