File and Directory classes contain different static methods for manipulating the files, directories, and subdirectories. These classes exist in the System.IO namespace. So, let’s inspect some of the most used methods of both classes.

For the complete navigation of this series check out: C# Back to Basics.

Support Code Maze on Patreon to get rid of ads and get the best discounts on our products!
Become a patron at Patreon!

File Methods

WriteAllText(string path, string contents) creates a new file and writes content to that file. If the target file already exists, it will overwrite it:

string path = @"C:\FileExamples\test.txt";
string content = "Example content as a string message";
File.WriteAllText(path, content);

WriteAllLines(string path, string[] contents) creates a new file and writes a specified string array, then it closes the file:

string path = @"C:\FileExamples\WriteAllLines.txt";
string[] contentArray = new string[3] { "Example content as a string message", "Another string text", "The last string" };
File.WriteAllLines(path, contentArray);

ReadAllText(string path) opens the file in the specified path, reads all the lines as a string, and then closes the file:

string path = @"C:\FileExamples\WriteAllLines.txt";
string readAllText = File.ReadAllText(path);
Console.WriteLine(readAllText);

ReadAllLines(string path) opens a text file, reads all lines of the file as a string array, and then closes the file:

string path = @"C:\FileExamples\WriteAllLines.txt";
string[] readAllLines = File.ReadAllLines(path);
foreach (string line in readAllLines)
{
    Console.WriteLine(line);
}

Delete(string path) Deletes the specified file:

string path = @"C:\FileExamples\test.txt";
File.Delete(path);

Move(string sourceFileName, string destFileName) moves a specified file to a new location:

string path = @"C:\FileExamples\test.txt";
string moveToPath = @"C:\FileMoveExamples\MovedFile.txt";

if(File.Exists(moveToPath)) //if the file on the target location exists, we need to remove it first.
{
    File.Delete(moveToPath);
}

File.Move(path, moveToPath);

AppendAllText(string path, string contents) opens a file, appends the content to the file, and then closes the file. If a file doesn’t exist, it will create a file, write the content, and close the file. This method is useful if we want to append new content without overriding the previous one:

string path = @"C:\FileExamples\test.txt";
string content = "Append this content as a string message" + Environment.NewLine;
File.AppendAllText(path, content);

AppendAllLines(string path, IEnumerable<string> contents) appends lines to the file and then closes the file:

string path = @"C:\FileExamples\test.txt";
string[] content = new string[2] { "Append this content as a string message", "Another text line" };
File.AppendAllLines(path, content); 

Directory Methods

CreateDirectory(string path) creates directories and subdirectories on the specified location unless they already exist. It returns a DirectoryInfo object for the existing directory:

string path = @"C:\DirectoryExample\SubDir1\SubDir2";
DirectoryInfo di = Directory.CreateDirectory(path);
Console.WriteLine($"Full name: {di.FullName}, Name: {di.Name}, Parent: {di.Parent} ...");

Delete(string path) deletes an empty directory from a specified path:

string path = @"C:\DirectoryExample\SubDir1\SubDir2";
Directory.Delete(path);

Delete(string path, bool recursive) deletes the specified directory, and if it is stated, all the subdirectories and files in that directory:

string path = @"C:\DirectoryExample";
Directory.Delete(path, true);

Move(string sourceDirName, string destDirName) moves a file or directory and its contents to a new location:

string path = @"C:\DirectoryExample";
string moveTo = @"C:\MoveDirectory";
Directory.Move(path, moveTo);

Conclusion

Well done. With this article, we have finished with the basics of C#. In our next module, we are going to talk about object-oriented concepts in C# and how to integrate it into our code.

Liked it? Take a second to support Code Maze on Patreon and get the ad free reading experience!
Become a patron at Patreon!