C# System.IO.DirectoryInfo Class
The System.IO.DirectoryInfo class represents a directory in your file system. Its capabilities are similar to the System.IO.Directory class. You need to create an instance of DirectoryInfo class to make use of its properties and methods.
DirectoryInfo directory = new DirectoryInfo(@"C:Directory");
The constructor of the DirectoryInfo class accepts a string argument which is the path of the directory that it will represent. You can then execute different methods such as creating, deleting, moving, and checking the details about the directory. You can also use some of the useful properties of this class as shown in the table below.
Properties | Description |
---|---|
Attributes | Gets the attributes of the directory. |
CreationTime | The date and time the directory was created. |
Exists | Returns true if the directory exists in the file system. |
LastAccessTime | The date and time the directory was last accessed. |
LastWriteTime | The date and time the directory was last modified. |
Name | Returns the name of the directory. |
FullName | Gets the full path of the directory or file. |
Parent | Gets the parent of the specified subdirectory. |
Root | Gets the root portion of the path. |
Figure 1 – System.IO.DirectoryInfo Properties
The Parent property returns the name of the parent directory of the DirectoryInfo intance. For example, the parent directory of C:DirectorySubdirectory is Directory. The Root property returns the root directory of the DirectoryInfo. For example, the root directory of C:Directory is C:.
The following are some of the methods of the DirectoryInfo class.
Properties | Description |
---|---|
Create | Creates a directory. |
CreateSubdirectory | Creates a subdirectory or subdirectories in the specified path. The path can be relative to the instance of System.IO.DirectoryInfo class. |
Delete | Deletes the directory. |
EnumerateDirectories | Returns a list of DirectoryInfo objects which are the subdirectories of the DirectoryInfo instance. |
EnumerateFiles | Returns a list of FileInfo objects which are the files located in the DirectoryInfo instance. |
GetDirectories | Same as EnumerateDirectories but returns the result as an array. |
GetFiles | Same as EnumerateFiles but returns the result as an array. |
MoveTo | Moves the directory to another directory. |
Figure 2 – System.IO.DirectoryInfo Properties
Creating Subdirectories
The CreateSubdirectory method is used to create subdirectories. Consider the following example.
DirectoryInfo directory = new DirectoryInfo(@"C:Directory");
directory.Create(); //Creates C:Directory
directory.CreateSubdirectory("Subdirectory"); //Creates C:DirectorySubdirectory
The path you give as the argument to the CreateSubdirectory method is a relative path. Which means, you don’t need to write the full path of the subdirectory. It will be relative to the path contained by the DirectoryInfo object.
Enumerating Directories and Files
You can use EnumerateDirectories, EnumerateFiles, GetDirectories, or GetFiles to retrieve a list of the directories and files contained by the directory of the DirectoryInfo instance. The difference of the two pairs is EnumereateDirectories and EnumerateFiles returns the result as a System.Collections.Generic.IEnumerable<T> where T is DirectoryInfo or FileInfo. GetDirectories and GetFiles returns the result as a simple DirectoryInfo or FileInfo array. The following code shows the list of files in a directory.
DirectoryInfo directory = new DirectoryInfo(@"C:");
IEnumerable<FileInfo> files = directory.EnumerateFiles();
foreach (FileInfo file in files)
{
Console.WriteLine(file.Name);
}
The output is not shown because the results will depend on the files contained in your C: directory.
You can use an overloaded of the four methods which accept a string argument that will serve as a search pattern or a filter to the results. For example, you can only list all the files with the extension .txt.
IEnumerable<FileInfo> textFiles = directory.EnumerateFiles("*.txt");
The * is a wildcard character that represents the name of the file. The textFiles will now contain a list of FileInfo objects that has a .txt extension.