C# - Directory And DirectoryInfo Class

Directory And DirectoryInfo Class

DirectoryInfo Class

The DirectoryInfo class provides various instance methods and properties for creating, deleting, and manipulating directories. The following table describes some of the common methods you can use to programmatically manipulate directories.

Method Description
Create Creates a directory.
CreateSubdirectory Creates a subdirectory.
Delete Deletes a directory.
GetDirectories Gets the subdirectories of the current directory.
GetFiles Gets the file list from a directory.

And here are some of the common properties:

Properties Description
Exists Indicates if a directory exists.
Parent Gets the parent of the current directory.
FullName Gets the full path name of the directory.
CreationTime Gets or sets the creation time of current directory.

To see how to use the DirectoryInfo class, consider the following example:

using System;
using System.IO;
namespace WorkingWithDirectoryInfo
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = @"C:\My Folder";
            DirectoryInfo di = new DirectoryInfo(path);
            try
            {
                //---if directory does not exists---
                if (!di.Exists)
                {
                    //---create the directory---
                    di.Create(); //---c:\My Folder---
                                 //---creates subdirectories---
                    di.CreateSubdirectory("Subdir1"); //---c:\My Folder\Subdir1---
                    di.CreateSubdirectory("Subdir2"); //---c:\My Folder\Subdir2---
                }
                //---print out some info about the directory---
                Console.WriteLine(di.FullName);
                Console.WriteLine(di.CreationTime);
                //---get and print all the subdirectories---
                DirectoryInfo[] subDirs = di.GetDirectories();
                foreach (DirectoryInfo subDir in subDirs)
                    Console.WriteLine(subDir.FullName);
                //---get the parent of C:\My folder---
                DirectoryInfo parent = di.Parent;
                if (parent.Exists)
                {
                    //---prints out C:\---
                    Console.WriteLine(parent.FullName);
                }
                //---creates C:\My Folder\Subdir3---
                DirectoryInfo newlyCreatedFolder =di.CreateSubdirectory("Subdir3");
                //---deletes C:\My Folder\Subdir3---
                newlyCreatedFolder.Delete();
            }
            catch (IOException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadKey();
        }
    }
}

Directory Class

The Directory class is similar to DirectoryInfo class. The key difference between is that Directory exposes static members instead of instance members. The Directory class also exposes only methods — no properties. Some of the commonly used methods are described in the following table.

Method Description
CreateDirectory Creates a subdirectory.
Delete Deletes a specified directory.
Exists Indicates if a specified path exists.
GetCurrentDirectory Gets the current working directory.
GetDirectories Gets the subdirectories of the specified path.
GetFiles Gets the file list from a specified directory.
SetCurrentDirectory Sets the current working directory.

Here’s the previous program using the DirectoryInfo class rewritten to use the Directory class:

using System;
using System.IO;
namespace WorkingWithDirectory
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = @"C:\My Folder";
            try
            {
                //---if directory does not exists---
                if (!Directory.Exists(path))
                {
                    //---create the directory---
                    Directory.CreateDirectory(path);
                    //---set the current directory to C:\My Folder---
                    Directory.SetCurrentDirectory(path);//---creates subdirectories---
                                                        //---c:\My Folder\Subdir1---
                    Directory.CreateDirectory("Subdir1");
                    //---c:\My Folder\Subdir2---
                    Directory.CreateDirectory("Subdir2");
                }
                //---set the current directory to C:\My Folder---
                Directory.SetCurrentDirectory(path);
                //---print out some info about the directory---
                Console.WriteLine(Directory.GetCurrentDirectory());
                Console.WriteLine(Directory.GetCreationTime(path));
                //---get and print all the subdirectories---
                string[] subDirs = Directory.GetDirectories(path);
                foreach (string subDir in subDirs)
                    Console.WriteLine(subDir);
                //---get the parent of C:\My folder---
                DirectoryInfo parent = Directory.GetParent(path);
                if (parent.Exists)
                {
                    //---prints out C:\---
                    Console.WriteLine(parent.FullName);
                }
                //---creates C:\My Folder\Subdir3---
                Directory.CreateDirectory("Subdir3");
                //---deletes C:\My Folder\Subdir3---
                Directory.Delete("Subdir3");
            }
            catch (IOException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadKey();
        }
    }
    
}