Working With The DirectoryInfo Class in C#



Introduction:

This article will explain the DirectoryInfo 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.

DirectoryInfo Class
The DirectoryInfo class shares almost all of the same properties as the FileInfo class, except that they operate on directories not files. The DirectoryInfo class does not have static methods and can only be used on instantiated objects. The DirectoryInfo object represents a physical directory on a disk. It also provides instance methods for the delete, create new directory and sub directories.  
When we use DirectoryInfo class
If you are going to reuse an object several times, consider using the instance method of DirectoryInfo instead of the corresponding static methods of the Directory class.
Some of the most useful methods of the DirectoryInfo class are:

S.No Methods Description
1 Create This method is used to create the new directory.
2 CreateSubdirectory This method is used to create a subdirectory or subdirectories on the specified path.
3 MoveTo Moves a DirectoryInfo instance and its contents to a new path.
4 Delete Deletes this instance of a DirectoryInfo, specifying whether to delete subdirectories and files.
5 GetDirectories
 
This method is used to get the sub directories in the given directory path.
6 GetFiles
 
The GetFiles method is used to get the files in the specified folder.

Figure1 DirectoryInfo Methods
Now Let us see the some of the methods in the DirectoryInfo class.
Create Method
This method is used to create the new directory.

static void Main(string[] args)

   {

    String path = @"D:\MyTestFile1.txt";

    DirectoryInfo fl = new DirectoryInfo(path);

       fl.Create();

        {

          Console.WriteLine("Directory has been created");

          }    

}

 

CreateSubDirectory Method

 

This method is used to create a subdirectory or subdirectories on the specified path.

 

static void Main(string[] args)

        {

            String path = @"D:\MyTestFile1.txt";

            DirectoryInfo fl = new DirectoryInfo(path);

            DirectoryInfo dis = fl.CreateSubdirectory("hellotest");

            {

            Console.WriteLine("Directory has been created");

            }    

        }

 

 

MoveTo Method

 

Moves a DirectoryInfo instance and its contents to a new path.

 

static void Main(string[] args)

        {

            String path = @"D:\MyTestFile1.txt";        

            string path1 = @"D:\NewFile1.txt";

            DirectoryInfo f1 = new DirectoryInfo(path);

            DirectoryInfo f2 = new DirectoryInfo(path1);

            f1.MoveTo(path1);

            {

            Console.WriteLine("Directory has been Moved");

            }    

        }

 

Delete Method

 

Deletes this instance of a DirectoryInfo, specifying whether to delete subdirectories and files.

 

static void Main(string[] args)

        {

           string path = @"D:\NewFile1.txt";

            DirectoryInfo f1 = new DirectoryInfo(path);

            f1.Delete();        

            {

            Console.WriteLine("Directory has been deleted");

            }    

        }

 

GetDirectories Method

 

This method is used to get the sub directories in the given directory path, or returns the subdirectories of the current directory.

 

static void Main(string[] args)

{

 try

{

 DirectoryInfo di = new DirectoryInfo(@"D:\newFile\");

 DirectoryInfo[] dir1 = di.GetDirectories();

 Console.WriteLine("The number of directories containing is {0}.", dir1.Length);

  }

   catch (Exception e)

      {

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

        }

 }

 

 

GetFiles

 

The GetFiles method is used to get the files in the specified folder.

 

static void Main(string[] args)

        {

            DirectoryInfo di = new DirectoryInfo(@"D:\newfile");

            DirectoryInfo[] dirs = di.GetDirectories();

            foreach (DirectoryInfo diNext in dirs)

            {

            Console.WriteLine("The number of files in {0} is {1}", diNext,

            diNext.GetFiles().Length);

            }

        }  

 

Example

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

 

namespace Createdirectory

{

    class Program

    {

        static void Main(string[] args)

        {

 

// Create Method

            String path = @"D:\MyTestFile1.txt";

            DirectoryInfo fl = new DirectoryInfo(path);

            fl.Create();

            {

                Console.WriteLine("Directory has been created");

            }

 

// CreateSubdirectory Method

 

            String path = @"D:\MyTestFile1.txt";

            DirectoryInfo fl = new DirectoryInfo(path);

            DirectoryInfo dis = fl.CreateSubdirectory("hellotest");

            {

                Console.WriteLine("Directory has been created");

            }

 

// MoveTo Method

 

            String path = @"D:\MyTestFile1.txt";

            string path1 = @"D:\NewFile1.txt";

            DirectoryInfo f1 = new DirectoryInfo(path);

            DirectoryInfo f2 = new DirectoryInfo(path1);

            f1.MoveTo(path1);

            {

                Console.WriteLine("Directory has been Moved");

            } 

   

// Delete Method

 

            string path = @"D:\NewFile1.txt";

            DirectoryInfo f1 = new DirectoryInfo(path);

            f1.Delete();

            {

                Console.WriteLine("Directory has been deleted");

            }    

 

// GetDirectories method

 

            try

            {

                DirectoryInfo di = new DirectoryInfo(@"D:\newFile\");

                DirectoryInfo[] dir1 = di.GetDirectories();

                Console.WriteLine("The number of directories containing is {0}.", dir1.Length);

            }

            catch (Exception e)

            {

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

            }

 

// GetFiles method

 

            DirectoryInfo di = new DirectoryInfo(@"D:\newfile");

            DirectoryInfo[] dirs = di.GetDirectories();

            foreach (DirectoryInfo diNext in dirs)

            {

                Console.WriteLine("The number of files in {0} is {1}", diNext,

                diNext.GetFiles().Length);

            }

        }     

    }

}

 

The DirectoryInfo class provides the following properties that enable you to retrieve information about a directory.

 

S.No Property Description
1 CreationTime This property returns the creation of the directory date and time.
2 Exists It returns whether the directory exists or not as Boolean result.
3 FullName It returns the full name of the file from the root directory.
4 Name It returns the name of the directory
5 LastAccessTime It returns the date and time of the last access time
6 LastWriteTime It returns the last file saving date and time
7 Root Gets the root portion of a path.

Figure 2  DirectoryInfo Properties
Property - Example

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

 

namespace directoryinfoproperty

{

    class Program

    {

        static void Main(string[] args)

        {

            DirectoryInfo fi = new DirectoryInfo(@"D:\newfile");

            Console.WriteLine("Directory name is {0} ", fi.Name);

            Console.WriteLine("Directory creation time is {0} ", fi.CreationTime.ToLongTimeString());

            Console.WriteLine("Directory Lastaccesstime is {0} ", fi.LastAccessTime.ToLongDateString());       

            Console.WriteLine("Directory exist is: ", fi.Exists);

            Console.WriteLine("Directory LastWriteTime is {0} ", fi.LastWriteTime);

            Console.WriteLine("Directory root is {0} ", fi.Root);              

        }

    }

}

 

 

Conclusion:

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

Up Next
    Ebook Download
    View all
    Learn
    View all