File and Directory Classes in C#
What are File and Directory Classes in C#?
The **File** and **Directory** classes in C# provide methods for performing operations on files and directories, such as **creating, deleting, copying, moving, and checking existence**. These classes are part of the **System.IO** namespace.
Key Features of File & Directory Classes
- Provide static methods for handling **files and folders**.
- Support operations like **creating, deleting, moving, and checking existence**.
- Can retrieve **file attributes, sizes, and timestamps**.
- Work seamlessly with **file paths and directories**.
Working with the File Class
The **File** class provides static methods for handling file operations.
Example: Creating, Writing, and Reading a File
using System;
using System.IO;
class Program
{
static void Main()
{
string filePath = "testfile.txt";
// Creating and writing to the file
File.WriteAllText(filePath, "Hello, File Class!");
// Checking if file exists
if (File.Exists(filePath))
{
Console.WriteLine("File created successfully.");
// Reading file content
string content = File.ReadAllText(filePath);
Console.WriteLine($"File Content: {content}");
}
}
}
// Output:
// File created successfully.
// File Content: Hello, File Class!
The **File.WriteAllText()** and **File.ReadAllText()** methods make file operations easy.
Copying, Moving, and Deleting Files
The **File** class allows copying, moving, and deleting files.
Example: Copying, Moving, and Deleting a File
using System;
using System.IO;
class Program
{
static void Main()
{
string sourceFile = "testfile.txt";
string copyFile = "copyfile.txt";
string movedFile = "movedfile.txt";
// Copy File
File.Copy(sourceFile, copyFile, true);
Console.WriteLine("File copied successfully.");
// Move File
File.Move(copyFile, movedFile);
Console.WriteLine("File moved successfully.");
// Delete File
File.Delete(movedFile);
Console.WriteLine("File deleted successfully.");
}
}
// Output:
// File copied successfully.
// File moved successfully.
// File deleted successfully.
The **File.Copy()**, **File.Move()**, and **File.Delete()** methods simplify file management.
Working with the Directory Class
The **Directory** class provides static methods to manage directories.
Example: Creating, Checking, and Deleting a Directory
using System;
using System.IO;
class Program
{
static void Main()
{
string directoryPath = "MyDirectory";
// Create Directory
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
Console.WriteLine("Directory created successfully.");
}
// Check if Directory Exists
if (Directory.Exists(directoryPath))
{
Console.WriteLine("Directory exists.");
}
// Delete Directory
Directory.Delete(directoryPath);
Console.WriteLine("Directory deleted successfully.");
}
}
// Output:
// Directory created successfully.
// Directory exists.
// Directory deleted successfully.
The **Directory.CreateDirectory()**, **Directory.Exists()**, and **Directory.Delete()** methods handle directory operations.
Listing Files and Directories
You can retrieve files and directories inside a folder using **Directory.GetFiles()** and **Directory.GetDirectories()**.
Example: Listing Files and Directories
using System;
using System.IO;
class Program
{
static void Main()
{
string directoryPath = "."; // Current directory
// Get all files in the directory
string[] files = Directory.GetFiles(directoryPath);
Console.WriteLine("Files in Directory:");
foreach (var file in files)
{
Console.WriteLine(file);
}
// Get all subdirectories
string[] directories = Directory.GetDirectories(directoryPath);
Console.WriteLine("\nSubdirectories:");
foreach (var dir in directories)
{
Console.WriteLine(dir);
}
}
}
// Output (depends on system files):
// Files in Directory:
// testfile.txt
// movedfile.txt
// Subdirectories:
// MyDirectory
// AnotherFolder
The **Directory.GetFiles()** method lists all files, while **Directory.GetDirectories()** lists subdirectories.
Best Practices for Using File & Directory Classes
- Use **File.Exists()** and **Directory.Exists()** before performing operations.
- Always **handle exceptions** when dealing with files and directories.
- Use **relative paths** instead of absolute paths when possible.
- Clean up **unused files and directories** to prevent clutter.