Basics Operations of File and Directory in C#

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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();
            }
        }
    }
}

chevron_right


Output:



Creating-Directory-CSharp

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

Renaming a Directory

filter_none

edit
close

play_arrow

link
brightness_4
code

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 exsist 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();
                }
            }
        }
    }
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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();
    }
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 exsist 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();
                }
            }
        }
    }
}

chevron_right


Output:

Renaming-File-CSharp

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

Deleting a File

filter_none

edit
close

play_arrow

link
brightness_4
code

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();
    }
}

chevron_right


Output:
Deleting-File-CSharp

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



My Personal Notes arrow_drop_up

Competitive Programmer

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.