C# Directoryinfo

The DirectoryInfo class is within the System.IO namespace and serves the purpose of managing directories. This class offers functionalities for creating, deleting, and relocating directories, as well as executing operations on directories and their subdirectories. DirectoryInfo is a sealed class, meaning it cannot be inherited.

The DirectoryInfo class offers a variety of constructors, methods, and properties as outlined:

C# DirectoryInfo Syntax

Example

[SerializableAttribute]
[ComVisibleAttribute(true)]
public sealed class DirectoryInfo : FileSystemInfo

C# DirectoryInfo Constructors

The subsequent table displays the constructors available in the DirectoryInfo class.

Constructor Description
DirectoryInfo(String) It is used to initialize a new instance of the DirectoryInfo class on the specified path.

C# DirectoryInfo Properties

The subsequent table displays the attributes of the DirectoryInfo class.

Property Description
Attributes It is used to get or set the attributes for the current file or directory.
CreationTime It is used to get or set the creation time of the current file or directory.
CreationTimeUtc It is used to get or set creation time, in coordinated universal time (UTC).
Exists It is used to get a value indicating whether the directory exists.
Extension It is used to get the string representing the extension part of the file.
FullName It is used to get the full path of the directory.
LastAccessTime It is used to get or set the time the current file or directory was last accessed.
LastAccessTimeUtc It is used to get or set the time, in coordinated universal time (UTC) that the current file or directory was last accessed.
LastWriteTime It is used to get or set the time when the current file or directory was last written.
LastWriteTimeUtc It is used to get or set the time, in coordinated universal time (UTC), when the current file or directory was last written.
Name It is used to get the name of this DirectoryInfo instance.
Parent It is used to get the parent directory of a specified subdirectory.
Root It is used to get the root portion of the directory.

C# DirectoryInfo Methods

The subsequent table displays the techniques of the DirectoryInfo class.

Method Description
Create() It is used to create a directory.
Create(DirectorySecurity) It is used to create a directory using a DirectorySecurity object.
CreateObjRef(Type) It is used to create an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.
CreateSubdirectory(String) It is used to create a subdirectory or subdirectories on the specified path.
CreateSubdirectory(String,DirectorySecurity) It is used to create a subdirectory or subdirectories on the specified path with the specified security.
Delete() It is used to delete this DirectoryInfo if it is empty.
Delete(Boolean) It is used to delete this instance of a DirectoryInfo, specifying whether to delete subdirectories and files.
EnumerateDirectories() It returns an enumerable collection of directory information in the current directory.
EnumerateFiles() It returns an enumerable collection of file information in the current directory.
GetAccessControl() It is used to get a DirectorySecurity object that encapsulates the access control list (ACL) entries for the directory.
GetDirectories() It returns the subdirectories of the current directory.
GetFiles() It returns a file list from the current directory.
GetType() It is used to get the Type of the current instance.
MoveTo(String) It is used to move a DirectoryInfo instance and its contents to a new path.
Refresh() It is used to refresh the state of the object.
SetAccessControl(DirectorySecurity) It is used to set access control list (ACL) entries described by a DirectorySecurity object.
ToString() It returns the original path that was passed by the user.

C# DirectoryInfo Example

In this instance, we are establishing a "hello world" directory by indicating the path to the directory.

Example

using System;
using System.IO;
namespace CSharpProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            // Provide directory name with complete location.
            DirectoryInfo directory = new DirectoryInfo(@"F:\hello world");
            try
            {
                // Check, directory exist or not.
                if (directory.Exists)
                {
                    Console.WriteLine("Directory already exist.");
                    return;
                }
                // Creating a new directory.
                directory.Create();
                Console.WriteLine("The directory is created successfully.");
            }
            catch (Exception e)
            {
                Console.WriteLine("Directory not created: {0}", e.ToString());
            }
        }
    }
}

Output:

Output

The directory is created successfully.

In the following image, a new directory has been successfully established.

The code snippet below illustrates the structure of a placeholder using CSS styling:

Example

.placeholder-diagram {
  background: linear-gradient(135deg, #374151 0%, #1f2937 100%);
  border-radius: 12px;
  padding: 40px;
  margin: 20px 0;
  text-align: center;
}

.placeholder-diagram .placeholder-icon {
  font-size: 3rem;
  margin-bottom: 10px;
}

.placeholder-diagram .placeholder-text {
  color: #9ca3af;
  font-size: 1rem;
}

The DirectoryInfo class includes a delete function to remove a directory that has been created. In the subsequent code, we are deleting a directory that was previously established.

C# DirectoryInfo Example: Deleting Directory

Example

using System;
using System.IO;
namespace CSharpProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            // Providing directory name with complete location.
            DirectoryInfo directory = new DirectoryInfo(@"F:\hello world");
            try
            {
                // Deleting directory
                directory.Delete();
                Console.WriteLine("The directory is deleted successfully.");
            }
            catch (Exception e)
            {
                Console.WriteLine("Something went wrong: {0}", e.ToString());
            }
        }
    }
}

Output:

Output

The directory is deleted successfully.

It triggers a System.IO.DirectoryNotFoundException error when the indicated directory is not found at the specified location.

Input Required

This code uses input(). Please provide values below: