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 different types of properties and methods to perform operations on directories and subdirectories to manipulate based on our requirements. 

C# DirectoryInfo Properties

The following are the different types of properties provided by the 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 indicating whether the directory exists or not.
Extension It is useful to get the extension part of the file.
FullName It provides the full path of a 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

The following are the different types of methods provided by DirectoryInfo class to perform different types 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 returns the subdirectories of the current directory.
GetFiles It returns the file list from the 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 to access DirectoryInfo class to check and create a directory.

 

When we execute the above example, it will create a new “Test” folder in the 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 the DirectoryInfo class to create a directory based on the specified path. Now, we will learn how to use the 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 to access the DirectoryInfo object to copy tutlane directory files and its subdirectories to the Test directory.

 

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

 

C# DirectoryInfo Copy Files to Another Directory Example Result

 

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