Working with Directory using C#


Introduction:

This article will explain about the Directory class and its use. The System.IO namespace is one of the most significant namespaces used for working with directories in the .Net Framework. Let us see about the class.


Directory Class

The Directory class exposes many static methods for moving, copying, and deleting directories. There are static methods that involve creating, moving and deleting directories. Some of the most useful static methods of the Directory class are:

S.No Method Description
1 CreateDirectory This method is used to create a directory in the specified path.
2 Delete This method is used to Delete a directory.
3 Exists It returns whether the directory exists or not as Boolean result.
4 Move Moves a specified directory to a new location. We can specify a different name for the file in the new location.
5 Getfiles Return an array of files objects in the current directory.
6 GetDirectories Returns an array of directory objects that represent the directories below the current directory.
7 GetCreationTime Gets the creation date and time of a directory.

CreateDirectory Method

This method is used to create a directory in the specified path.

public static void Main()

    {

        string dirpath = @"D:\Testdir";

        try

        {          

            if (Directory.Exists(dirpath))

            {

                Console.WriteLine("That path exists already.");

                return;

            }

 

            Directory.CreateDirectory(dirpath);

            Console.WriteLine("The directory was created successfully at {0}.", Directory.GetCreationTime(dirpath));         

        }

        catch (Exception e)

        {

            Console.WriteLine("The process failed: {0}", e.ToString());

        }    

    }


Delete Method

This method is used to Delete a directory.

public static void Main()

    {

        string dirpath = @"D:\Testdir";

        try

        {

            Directory.Delete(dirpath);

            Console.WriteLine("The directory was deleted successfully.");       

        }

        catch (Exception e)

        {

            Console.WriteLine("The process failed: {0}", e.ToString());

        }    

    }


Exists Method

It returns whether the directory exists or not as Boolean result.

string dirpath = @"D:\Testdir";

        try

        {          

            if (Directory.Exists(dirpath))

            {

                Console.WriteLine("That path exists already.");

                return;

            }                

        }

        catch (Exception e)

        {

            Console.WriteLine("The process failed: {0}", e.ToString());

        }    

   


Move Method

This method is used to Move a specified directory to a new location. We can specify a different name for the file in the new location.

public static void Main()

    {

        string path = @"e:\MyTestFile1.txt";

        string path1 = @"e:\MyTest1.txt";

        Directory.Move(path, path1);

        Console.WriteLine("File has been moved");

    }

 

GetCreationTime Method

 

This method is used to get the date and time a directory was created.

 

public static void Main()

    {

        string dirpath = @"D:\Testdir4";       

        {

            try

            {

                if (Directory.Exists(dirpath))

                {

                    Console.WriteLine("That path exists already.");

                    return;

                }

 

                Directory.CreateDirectory(dirpath);

                Console.WriteLine("The directory was created successfully at {0}.", Directory.GetCreationTime(dirpath));               

            }

 

            catch (Exception e)

            {

                Console.WriteLine("The process failed: {0}", e.ToString());

            }

        }

    }

 

Example

 

using System;

using System.IO;

class Test

{

    public static void Main()

    {

        string dirpath = @"D:\Testdir4";

        try

        {

            if (Directory.Exists(dirpath))

            {

                Console.WriteLine("That path exists already.");

                return;

            }

 

            Directory.CreateDirectory(dirpath);

            Console.WriteLine("The directory was created successfully at {0}.", Directory.GetCreationTime(dirpath));

            Directory.Delete(dirpath);

            Console.WriteLine("The directory was deleted successfully.");  

        }

          

        catch (Exception e)

        {

            Console.WriteLine("The process failed: {0}", e.ToString());

        }

    }

 

}

Getfiles Method

This method is used to return an array of files objects in the current directory.

GetDirectories Method

This is used to return an array of directory objects that represent the directories below the current directory.

Conclusion:

The Directory class is very useful for working with files. If there is any mistake in this article then please notify me. I expect your valuable comments and feedback about this article.


Similar Articles