C# DirectoryInfo

In c#, DirectoryInfo is a class of System.IO namespace and it is useful to perform file operations such as creating, renaming, moving, copying and deleting directories and subdirectories.

 

In c#, the DirectoryInfo class is not inheritable and it’s having a different type of properties and methods to perform operations on directories and subdirectories to manipulate based on our requirements. 

C# DirectoryInfo Properties

Following are the different type of properties which are provided by DirectoryInfo class to retrieve the information about files.

 

PropertyDescription
CreationTime It is useful to get or set the creation time of the current file or directory.
Exists It provides a value which indicating whether the directory exists or not.
Extension It is useful to get the extension part of file.
FullName It provide the full path of directory.
LastAccessTime It is useful to gets or sets the time the current file or directory was last accessed.
Parent It is useful to get the parent directory of a specified subdirectory.

C# DirectoryInfo Methods

Following are the different type of methods which are provided by DirectoryInfo class to perform a different type of operations on directories and subdirectories.

 

MethodDescription
Create This method will create a directory.
Delete This method will delete the specified directory.
EnumerateDirectories It returns an enumerable collection of directory information in the current directory.
GetDirectories It return the subdirectories of current directory.
GetFiles It return the file list from current directory.
MoveTo It moves a specified directory and its contents to a new path.
ToString This will return the original path that was passed by the user.

Now, we will see how to use DirectoryInfo class in c# to create, delete, read, move and open operations on directories with examples.

C# FileInfo Class Create Directory Example

Following is the example of creating the directory using DirectoryInfo class in c#.

 

using System;

using System.IO;

 

namespace TutlaneExamples

{

    class Program

    {

        static void Main(string[] args)

        {

            //Specify the directory which we want to manipulate

            string fpath = @"D:\Test";

            DirectoryInfo di = new DirectoryInfo(fpath);

            // Check if directory exists

            if (di.Exists)

            {

                Console.WriteLine("Directory Already Exists");

            }

            else

            {

                // Create directory

                di.Create();

                Console.WriteLine("Directory Created Successfully");

            }

            Console.ReadLine();

        }

    }

}

If you observe the above example, we imported a System.IO namespace in our example to access DirectoryInfo class to check and create a directory.

 

When we execute the above example, it will create a new “Test” folder in D drive like as shown below.

 

C# DirectoryInfo Create Directory Example Result

 

This is how we can check and create directories using DirectoryInfo class in c#.

C# DirectoryInfo Class Copy Directory Example

In the previous example, we learned how to use DirectoryInfo class to create a directory based on the specified path. Now, we will learn how to use DirectoryInfo class to copy the directory and its content to another directory with an example.

 

using System;

using System.IO;

 

namespace TutlaneExamples

{

    class Program

    {

        static void Main(string[] args)

        {

            //Specify the directory which we want to manipulate

            string sourcedir = @"D:\tutlane";

            string targetdir = @"D:\Test";

            DirectoryInfo sdi = new DirectoryInfo(sourcedir);

            DirectoryInfo tdi = new DirectoryInfo(targetdir);

            // Check if target directory exists, if not, create it

            if (!tdi.Exists)

            {

                tdi.Create();

            }

            // Copy each file into it's new directory.

            foreach (FileInfo fi in sdi.GetFiles())

            {

                fi.CopyTo(Path.Combine(tdi.ToString(), fi.Name), true);

                Console.WriteLine(@"Copying {0}\{1}", tdi.FullName, fi.Name);

            }

 

            // Copy each subdirectory and it's files

            foreach (DirectoryInfo sourceSubDir in sdi.GetDirectories())

            {

                DirectoryInfo targetSubDir =

                    tdi.CreateSubdirectory(sourceSubDir.Name);

                // Copy each file into it's new directory.

                foreach (FileInfo fi in sourceSubDir.GetFiles())

                {

                    fi.CopyTo(Path.Combine(targetSubDir.ToString(), fi.Name), true);

                    Console.WriteLine(@"Copying {0}\{1}", targetSubDir.FullName, fi.Name);

                }

            }

 

            Console.ReadLine();

        }

    }

}

If you observe the above example, we imported a System.IO namespace in our example to access DirectoryInfo object to copy tutlane directory files and it’s subdirectories to the Test directory.

 

When we execute the above example, the “tutlane” directory files and its subdirectories will be copied to “Test” directory like as shown below.

 

C# DirectoryInfo Copy Files to Another Directory Example Result

 

This is how we can use DirectoryInfo class in c# to create, copy, rename and delete directories and subdirectories based on our requirements.

 

PREVIOUS

C# FileInfo
 
 

Topics Covered