Directory Class

System.IO

Summary

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

Syntax

public static class Directory

Remarks

The Directory class provides methods for performing common operations on directories. These operations include creating, deleting, moving, and enumerating the contents of directories. All methods in the Directory class are static, meaning you do not need to create an instance of the class to use them.

For object-oriented operations on directories, use the DirectoryInfo class.

Example

The following C# code example demonstrates how to create a directory, create a file inside it, and then delete the directory.


using System;
using System.IO;

public class DirectoryExample
{
    public static void Main(string[] args)
    {
        string dirPath = @"C:\MyTestDirectory";
        string filePath = Path.Combine(dirPath, "MyTestFile.txt");

        try
        {
            // Create the directory
            Directory.CreateDirectory(dirPath);
            Console.WriteLine($"Directory '{dirPath}' created successfully.");

            // Create a file inside the directory
            File.WriteAllText(filePath, "This is a test file.");
            Console.WriteLine($"File '{filePath}' created successfully.");

            // List all files in the directory
            string[] files = Directory.GetFiles(dirPath);
            Console.WriteLine($"Files in '{dirPath}':");
            foreach (string file in files)
            {
                Console.WriteLine($"- {file}");
            }

            // Delete the file and then the directory
            File.Delete(filePath);
            Console.WriteLine($"File '{filePath}' deleted.");

            Directory.Delete(dirPath);
            Console.WriteLine($"Directory '{dirPath}' deleted.");
        }
        catch (IOException e)
        {
            Console.WriteLine($"An error occurred: {e.Message}");
        }
    }
}
                

Members

Name Description
Method
CreateDirectory
Creates all the necessary directories and subdirectories for the specified path.
Method
Delete
Deletes an empty directory.
Method
Delete(String, Boolean)
Deletes a directory, including any subdirectories and files.
Method
Exists
Determines whether the given path refers to an existing directory on disk.
Method
GetCreationTime
Gets the creation date and time of the specified file or directory.
Method
GetCurrentDirectory
Gets the current working directory of the application.
Method
GetDirectories
Returns the names of subdirectories (including their paths) in the specified directory.
Method
GetFiles
Returns the names of all files (including their paths) in the specified directory.
Method
GetLastAccessTime
Gets the date and time, in Coordinated Universal Time (UTC), of the last access to the specified file or directory.
Method
GetLastWriteTime
Gets the date and time the specified file or directory was last written to.
Method
Move
Moves a specified directory and its contents to a new location.
Method
SetCreationTime
Sets the creation date and time of the specified file or directory.
Method
SetCurrentDirectory
Changes the current working directory to the specified path.

Exceptions

  • ArgumentException
  • ArgumentNullException
  • DirectoryNotFoundException
  • IOException
  • UnauthorizedAccessException