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
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.
To use the Directory
class, you need to import the System.IO
namespace:
using System.IO;
To create a new directory, use the CreateDirectory()
method:
string folderPath = @"C:\MyNewFolder"; Directory.CreateDirectory(folderPath);
To check if a directory exists, use the Exists()
method:
bool directoryExists = Directory.Exists(folderPath);
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);
To move a directory to a new location, use the Move()
method:
string sourcePath = @"C:\MyNewFolder"; string destinationPath = @"C:\MyNewFolderMoved"; Directory.Move(sourcePath, destinationPath);
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#.
C# Directory class example:
Directory
class in C# provides static methods for working with directories (folders) in the file system.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."); } } }
Creating a directory in C#:
Directory.CreateDirectory
method is used to create a new directory.string newDirectoryPath = @"C:\NewDirectory"; Directory.CreateDirectory(newDirectoryPath);
Checking if a directory exists in C#:
Directory.Exists
method is used to check if a directory exists.string directoryPath = @"C:\ExistingDirectory"; if (Directory.Exists(directoryPath)) { Console.WriteLine("Directory exists!"); }
Listing files in a directory with C# Directory class:
Directory.GetFiles
method is used to retrieve an array of file names in a directory.string directoryPath = @"C:\SomeDirectory"; string[] files = Directory.GetFiles(directoryPath); foreach (string file in files) { Console.WriteLine(file); }
Deleting a directory in C#:
Directory.Delete
method is used to delete a directory.string directoryToDelete = @"C:\DirectoryToDelete"; Directory.Delete(directoryToDelete);
C# Directory class for moving folders:
Directory.Move
method allows you to move a directory to a new location.string sourceDirectory = @"C:\SourceDirectory"; string destinationDirectory = @"D:\DestinationDirectory"; Directory.Move(sourceDirectory, destinationDirectory);
Getting directory information in C#:
DirectoryInfo
class provides more detailed information about a directory.string directoryPath = @"C:\SomeDirectory"; DirectoryInfo directoryInfo = new DirectoryInfo(directoryPath); Console.WriteLine($"Directory Name: {directoryInfo.Name}"); Console.WriteLine($"Creation Time: {directoryInfo.CreationTime}");
Renaming a directory in C#:
Directory.Move
method can be used to rename a directory by moving it to a new location with a different name.string oldDirectoryName = @"C:\OldName"; string newDirectoryName = @"C:\NewName"; Directory.Move(oldDirectoryName, newDirectoryName);