Sabari M Sabari M
Updated date Dec 15, 2020
In this blog, we will see how to create, move, copy, and delete a directory using C# classes and methods.

The Directory is a static class that provides static methods that are used to create, delete, copy, move, and enumerate through directories and subdirectories. If you want to use the Directory class, you need to import the System.IO namespace.

Create a Directory in C#:

Below example shows how to create a directory,

using System;
using System.IO;  // Must add this namespace
namespace ConsoleApp
{
    class DirectoryAndFile
    {
        static void Main(string[] args)
        {
            string directoryName = @"C:\Employee";            

            try
            {
                // Checking if the directory is exists or not. If not, create a directory/folder
                if (Directory.Exists(directoryName))
                {
                    // Create a Directory/folder
                    Directory.CreateDirectory(directoryName);
                }                
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }  
}

Delete a Directory in C#:

The below code explains to delete an existing directory/folder from disk,

using System;
using System.Text;
using System.IO; // Must add this namespace
namespace ConsoleApp
{
    class DirectoryAndFile
    {
        static void Main(string[] args)
        {
            string sourceDirectory = @"C:\Employee";           

            try
            {
                // Checking if the directory is exists. If yes then delete a directory/folder
                if (Directory.Exists(sourceDirectory))
                {
                    // Delete a Directory/folder
                    Directory.Delete(sourceDirectory);
                }                
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }  
}

Move Files from a Directory in C#:

The following example moves the specific files (in our case *.jpg file) from the source directory to the destination directory. The will not be available in the source directory once all files are successfully moved.

using System;
using System.IO;  // Must add this namespace
namespace ConsoleApp
{
    class DirectoryAndFile
    {
        static void Main(string[] args)
        {
            string srcDirectory = @"C:\Employee";
            string desDirectory = @"C:\EmployeeBackup";
            try
            {
                // Get all the jpg files from a Directory
                var allFiles = Directory.EnumerateFiles(srcDirectory, "*.jpg");
                // Loop all the jpg files one by one
                foreach (string file in allFiles)
                {
                    string fileName = file.Substring(srcDirectory.Length + 1);
                    // Move the file to destination directory/folder
                    Directory.Move(file, Path.Combine(desDirectory, fileName));
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }  
}

To move all the files from the source directory to the destination directory,

using System;
using System.IO; // Must add this namespace
namespace ConsoleApp
{
    class DirectoryAndFile
    {
        static void Main(string[] args)
        {
            string srcDirectory = @"C:\Employee";
            string destDirectory = @"C:\EmpBackup";
            try
            {
                // Move all the files from source to destination directory
                Directory.Move(srcDirectory, destDirectory);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }  
}

Get first level directories from Parent directory:

using System;
using System.Collections.Generic;
using System.IO; // Must add this namespace
namespace ConsoleApp
{
    class DirectoryAndFile
    {
        static void Main(string[] args)
        {
            try
            {
                // Source directory path
                string docPath = @"C:\Employee";

                // Get all the 1 level directory list
                List<string> directories = new List<string>(Directory.EnumerateDirectories(docPath));
                // Loop the drectory one by one
                foreach (var directory in directories)
                {
                    // Display directory Name
                    Console.WriteLine($"{directory.Substring(directory.LastIndexOf(Path.DirectorySeparatorChar) + 1)}");
                }
                // Display Directory count
                Console.WriteLine($"{directories.Count} directories found.");
                Console.ReadLine();
            }
            catch (UnauthorizedAccessException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (PathTooLongException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }  
}

Output :

ABOUT THE AUTHOR

Sabari M
Sabari M
Software Professional, India

IT professional with 14+ years of experience in Microsoft Technologies with a strong base in Microsoft .NET (C#.Net, ASP.Net MVC, ASP.NET WEB API, Webservices,...Read More

https://www.techieclues.com/profile/alagu-mano-sabari-m

Comments (0)

There are no comments. Be the first to comment!!!