fastread homefastrread library fastread menu

C# : Manipulating files and directories

Tutorial by:Manisha Dubey      Date: 2016-06-21 01:13:58

❰ Previous Next ❱

In the previous section, we looked into reading and writing text with simple files. We used the File class for this, but it does a lot more than just reading and writing. When combined with the Directory class, we can perform pretty much any filesystem operation, like renaming, moving, deleting and so on.

This section will provide numerous examples of doing just those things. The explanations will be limited, since the methods used are pretty simple and easy to use. You should be aware of two things: First of all, make sure that you import the System.IO namespace like this:

using System.IO;

Also, be aware that we are not doing any exception handling here. We will check that files and directories exist before using it, but there's no exception handling, so in case anything goes wrong, the application will crash. Exception handling is generally a good idea when doing IO operations. For more information, please read the exception handling chapter in this tutorial.

In all of the examples, we use simple file and directory names, which will have to exist in the same directory as the generated EXE file of your application. In the project settings you can see where your EXE file is generated to.

Deleting a file

if(File.Exists("test.txt"))
{
    File.Delete("test.txt");
    if(File.Exists("test.txt") == false)
        Console.WriteLine("File deleted...");
}
else
    Console.WriteLine("File test.txt does not yet exist!");
Console.ReadKey();

Deleting a directory

if(Directory.Exists("testdir"))
{
    Directory.Delete("testdir");
    if(Directory.Exists("testdir") == false)
        Console.WriteLine("Directory deleted...");
}
else
    Console.WriteLine("Directory testdir does not yet exist!");
Console.ReadKey();

If testdir is not empty, you will receive an exception. Why? Because this version of Delete() on the Directory class only works on empty directories. It's very easy to change though:

Directory.Delete("testdir", true);

The extra parameter makes sure that the Delete() method is recursive, meaning that it will traverse subdirectories and delete the files of it, before deleting the directories.

Renaming a file

if(File.Exists("test.txt"))
{
    Console.WriteLine("Please enter a new name for this file:");
    string newFilename = Console.ReadLine();
    if(newFilename != String.Empty)
    {
        File.Move("test.txt", newFilename);
        if(File.Exists(newFilename))
        {
            Console.WriteLine("The file was renamed to " + newFilename);
            Console.ReadKey();
        }
    }
}

You will notice that we use the Move() method to rename the file. Why not a Rename() method? Because there are no such method, since moving and renaming is essentially the same thing.

Renaming a directory

if(Directory.Exists("testdir"))
{
    Console.WriteLine("Please enter a new name for this directory:");
    string newDirName = Console.ReadLine();
    if(newDirName != String.Empty)
    {
        Directory.Move("testdir", newDirName);
        if(Directory.Exists(newDirName))
        {
            Console.WriteLine("The directory was renamed to " + newDirName);
            Console.ReadKey();
        }
    }
}

Creating a new directory

Console.WriteLine("Please enter a name for the new directory:");
string newDirName = Console.ReadLine();
if(newDirName != String.Empty)
{
    Directory.CreateDirectory(newDirName);
    if(Directory.Exists(newDirName))
    {
        Console.WriteLine("The directory was created!");
        Console.ReadKey();
    }
}

❰ Previous Next ❱


C#

Submit Your Thought, Tutorial, Articls etc.

Submit Your Information India's Number one online promotion website