DirectoryInfo class is a part of System.IO namespace. It is used to create, delete and move directory. It provides methods to perform operations related to directory and subdirectory. It is a sealed class so, we cannot inherit it.
The DirectoryInfo class provides constructors, methods and properties that are listed below.
[SerializableAttribute] [ComVisibleAttribute(true)] public sealed class DirectoryInfo : FileSystemInfo
The following table contains the constructors for the DirectoryInfo class.
Constructor | Description |
---|---|
DirectoryInfo(String) | It is used to initialize a new instance of the DirectoryInfo class on the specified path. |
The following table contains the properties 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. |
The following table contains the methods 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. |
In the following example, we are creating a rookienerd directory by specifying the directory path.
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:\rookienerd"); 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()); } } } }
In below screenshot, we can see that a directory is created.
The DirectoryInfo class also provides a delete method to delete created directory. In the following program, we are deleting a directory that we created in previous program.
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:\rookienerd"); try { // Deleting directory directory.Delete(); Console.WriteLine("The directory is deleted successfully."); } catch (Exception e) { Console.WriteLine("Something went wrong: {0}", e.ToString()); } } } }
It throws a System.IO.DirectoryNotFoundException exception if the specified directory not present at the location.