C# Tutorial

C# String

C# Array

C# Flow Control

C# Class and Object

C# Inheritance

C# Interface

C# Collection

C# Generic

C# File I/O

C# Delegate and Event

C# Exception

C# Process and Thread

C# ADO.NET Database Operations

C# Directory Class: Folder Operations

In C#, the Directory class, part of the System.IO namespace, provides methods for working with directories (folders). In this tutorial, we'll cover the most common folder operations using the Directory class.

  • Importing the System.IO namespace:

To use the Directory class, you need to import the System.IO namespace:

using System.IO;
  • Creating a directory:

To create a new directory, use the CreateDirectory() method:

string folderPath = @"C:\MyNewFolder";
Directory.CreateDirectory(folderPath);
  • Checking if a directory exists:

To check if a directory exists, use the Exists() method:

bool directoryExists = Directory.Exists(folderPath);
  • Getting a list of files and subdirectories:

To get an array of file and subdirectory names in a directory, use the GetFiles() and GetDirectories() methods:

string[] files = Directory.GetFiles(folderPath);
string[] subdirectories = Directory.GetDirectories(folderPath);
  • Moving a directory:

To move a directory to a new location, use the Move() method:

string sourcePath = @"C:\MyNewFolder";
string destinationPath = @"C:\MyNewFolderMoved";
Directory.Move(sourcePath, destinationPath);
  • Deleting a directory:

To delete a directory, use the Delete() method. Note that by default, this method throws an exception if the directory is not empty. To delete a non-empty directory, pass true as the second parameter:

string folderToDelete = @"C:\MyNewFolderMoved";
Directory.Delete(folderToDelete, true);

Here's a complete example demonstrating folder operations using the Directory class in C#:

using System;
using System.IO;

namespace DirectoryTutorial
{
    class Program
    {
        static void Main(string[] args)
        {
            string folderPath = @"C:\MyNewFolder";

            // Creating a directory
            Console.WriteLine("Creating directory...");
            Directory.CreateDirectory(folderPath);

            // Checking if a directory exists
            bool directoryExists = Directory.Exists(folderPath);
            Console.WriteLine($"Directory exists: {directoryExists}");

            // Getting a list of files and subdirectories
            string[] files = Directory.GetFiles(folderPath);
            string[] subdirectories = Directory.GetDirectories(folderPath);
            Console.WriteLine("Files and subdirectories:");
            foreach (string file in files)
            {
                Console.WriteLine(file);
            }
            foreach (string subdir in subdirectories)
            {
                Console.WriteLine(subdir);
            }

            // Moving a directory
            Console.WriteLine("Moving directory...");
            string destinationPath = @"C:\MyNewFolderMoved";
            Directory.Move(folderPath, destinationPath);

            // Deleting a directory
            Console.WriteLine("Deleting directory...");
            Directory.Delete(destinationPath, true);

            // Verifying the directory was deleted
            bool directoryDeleted = !Directory.Exists(destinationPath);
            Console.WriteLine($"Directory deleted: {directoryDeleted}");
        }
    }
}

This example demonstrates how to perform common folder operations using the Directory class in C#.

  1. C# Directory class example:

    • Description: The Directory class in C# provides static methods for working with directories (folders) in the file system.
    • Code:
      using System;
      using System.IO;
      
      class Program
      {
          static void Main()
          {
              // Example using Directory class
              string directoryPath = @"C:\ExampleDirectory";
      
              if (Directory.Exists(directoryPath))
              {
                  Console.WriteLine("Directory exists!");
              }
              else
              {
                  Console.WriteLine("Directory does not exist.");
              }
          }
      }
      
  2. Creating a directory in C#:

    • Description: The Directory.CreateDirectory method is used to create a new directory.
    • Code:
      string newDirectoryPath = @"C:\NewDirectory";
      Directory.CreateDirectory(newDirectoryPath);
      
  3. Checking if a directory exists in C#:

    • Description: The Directory.Exists method is used to check if a directory exists.
    • Code:
      string directoryPath = @"C:\ExistingDirectory";
      if (Directory.Exists(directoryPath))
      {
          Console.WriteLine("Directory exists!");
      }
      
  4. Listing files in a directory with C# Directory class:

    • Description: The Directory.GetFiles method is used to retrieve an array of file names in a directory.
    • Code:
      string directoryPath = @"C:\SomeDirectory";
      string[] files = Directory.GetFiles(directoryPath);
      foreach (string file in files)
      {
          Console.WriteLine(file);
      }
      
  5. Deleting a directory in C#:

    • Description: The Directory.Delete method is used to delete a directory.
    • Code:
      string directoryToDelete = @"C:\DirectoryToDelete";
      Directory.Delete(directoryToDelete);
      
  6. C# Directory class for moving folders:

    • Description: The Directory.Move method allows you to move a directory to a new location.
    • Code:
      string sourceDirectory = @"C:\SourceDirectory";
      string destinationDirectory = @"D:\DestinationDirectory";
      Directory.Move(sourceDirectory, destinationDirectory);
      
  7. Getting directory information in C#:

    • Description: The DirectoryInfo class provides more detailed information about a directory.
    • Code:
      string directoryPath = @"C:\SomeDirectory";
      DirectoryInfo directoryInfo = new DirectoryInfo(directoryPath);
      
      Console.WriteLine($"Directory Name: {directoryInfo.Name}");
      Console.WriteLine($"Creation Time: {directoryInfo.CreationTime}");
      
  8. Renaming a directory in C#:

    • Description: The Directory.Move method can be used to rename a directory by moving it to a new location with a different name.
    • Code:
      string oldDirectoryName = @"C:\OldName";
      string newDirectoryName = @"C:\NewName";
      Directory.Move(oldDirectoryName, newDirectoryName);