Open In App

Basics Operations of File and Directory in C#

Last Updated : 13 Sep, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to cover how to create, delete and rename directory and also how to delete and rename the file. 
 

Creating a Directory

We can create Directory using CreateDirectory() method present in the Directory class.
 

csharp




// C# program to create a directory
using System;
using System.IO;
 
class Program {
 
    // Main Method
    static void Main(string[] args)
    {
        Console.WriteLine("Please enter a name for the new directory:");
 
        string DirName = Console.ReadLine();
 
        // Checking if string is empty or not
        if (DirName != String.Empty)
        {
            // Creating the Directory
            Directory.CreateDirectory(DirName);
 
            // Checking Directory is created
            // Successfully or not
            if (Directory.Exists(DirName))
            {
                Console.WriteLine("The directory was created!");
                Console.ReadKey();
            }
        }
    }
}


Output:
 

Creating-Directory-CSharp

You will find the Directory with a given name at the specific location. 
 

Renaming a Directory

 

csharp




using System;
using System.IO;
 
class GFG {
 
    // Main Method
    static void Main(string[] args)
    {
        Console.WriteLine("Please enter a  name of the directory to renamed:");
        string DirName = Console.ReadLine();
 
        // checking directory exist or not
        if (Directory.Exists(DirName))
        {
            Console.WriteLine("Please enter a new name for this directory:");
            string newDirName = Console.ReadLine();
 
            if (newDirName != String.Empty) {
 
                // to rename directory
                Directory.Move(DirName, newDirName);
 
                // checking directory has
                // been renamed or not
                if (Directory.Exists(newDirName))
                {
                    Console.WriteLine("The directory was renamed to " + newDirName);
                    Console.ReadKey();
                }
            }
        }
    }
}


Output:
 

Renaming-Directory-CSharp

You will find the updated Directory’s name at the specific location. There is no such method called Rename() so we are using the Move() method to rename a directory. Moving and renaming are the same operations in C#.
 

Deleting a Directory

 

csharp




using System;
using System.IO;
 
class GFG {
 
    // Main Method
    static void Main(string[] args)
    {
        Console.WriteLine("Enter the directory name you want to delete:");
 
        string DirName = Console.ReadLine();
 
        // Checking if Directory Exist or not
        if (Directory.Exists(DirName))
        {
 
            // This will delete the
            // Directory if it is empty
            Directory.Delete(DirName);
 
            // checking if directory if
            // deleted successfully or not
            if (Directory.Exists(DirName) == false)
                Console.WriteLine("Directory deleted successfully...");
        }
        else
            Console.WriteLine("Directory {0} does not exist!", DirName);
        Console.ReadKey();
    }
}


Output:
 

Deleting-Directory-CSharp

You will find the Directory with a given name does not exist anymore at the specified location. If Directory is not empty, then Delete() will throw an exception because it deletes an only empty directory.
 

Directory.Delete(DirName, true);

If we pass the extra parameters then the Delete() method is recursive. First, all the files and subdirectories of the specified directory are deleted before deleting the directory. 
 

Renaming a File

 

csharp




// C# Program for Renaming a file
using System;
using System.IO;
 
class GFG {
 
    // Main Method
    static void Main(string[] args)
    {
        Console.WriteLine("Please enter a  name of the file to renamed:");
        string FileName = Console.ReadLine();
 
        // Checking File exist or not
        if (File.Exists(FileName))
        {
            Console.WriteLine("Please enter a new name for this file:");
            string newFilename = Console.ReadLine();
 
            // Checking if string is null or not
            if (newFilename != String.Empty)
            {
           
                // Renaming the file
                File.Move(FileName, newFilename);
 
                // checking if the file has been
                // renamed successfully or not
                if (File.Exists(newFilename))
                {
                    Console.WriteLine("The file was renamed to " + newFilename);
                    Console.ReadKey();
                }
            }
        }
    }
}


Output:
 

Renaming-File-CSharp

You will find the changed file name at the specific location.
 

Deleting a File

 

csharp




using System;
using System.IO;
 
class GFG {
 
    // Main Method
    static void Main(string[] args)
    {
        Console.WriteLine("Enter the file name you want to delete:");
        string FileName = Console.ReadLine();
 
        // Checking file exists or not
        if (File.Exists(FileName))
        {
 
            // Deleting the file
            File.Delete(FileName);
  
            // Checking if the file is deleted
            // successfully or not
            if (File.Exists(FileName) == false)
                Console.WriteLine("File deleted successfully...");
        }
        else
            Console.WriteLine("File {0} does not exist!", FileName);
        Console.ReadKey();
    }
}


Output: 
 

Deleting-File-CSharp

You will find there is no such file exists with a given name at the specific location.
 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads