In c#, the DirectoryInfo class is not inheritable, and it’s having different types of properties and methods to perform operations on directories and subdirectories to manipulate based on our requirements.
The following are the different types of properties provided by the DirectoryInfo class to retrieve the information about files.
|CreationTime||It is useful to get or set the creation time of the current file or directory.|
|Exists||It provides a value indicating whether the directory exists or not.|
|Extension||It is useful to get the extension part of the file.|
|FullName||It provides the full path of a directory.|
|LastAccessTime||It is useful to gets or sets the time the current file or directory was last accessed.|
|Parent||It is useful to get the parent directory of a specified subdirectory.|
The following are the different types of methods provided by DirectoryInfo class to perform different types of operations on directories and subdirectories.
|Create||This method will create a directory.|
|Delete||This method will delete the specified directory.|
|EnumerateDirectories||It returns an enumerable collection of directory information in the current directory.|
|GetDirectories||It returns the subdirectories of the current directory.|
|GetFiles||It returns the file list from the current directory.|
|MoveTo||It moves a specified directory and its contents to a new path.|
|ToString||This will return the original path that was passed by the user.|
Now, we will see how to use DirectoryInfo class in c# to create, delete, read, move and open operations on directories with examples.
Following is the example of creating the directory using DirectoryInfo class in c#.
When we execute the above example, it will create a new “Test” folder in the D drive like as shown below.
This is how we can check and create directories using DirectoryInfo class in c#.
In the previous example, we learned how to use the DirectoryInfo class to create a directory based on the specified path. Now, we will learn how to use the DirectoryInfo class to copy the directory and its content to another directory with an example.
When we execute the above example, the “tutlane” directory files and their subdirectories will be copied to the “Test” directory like as shown below.
This is how you can use DirectoryInfo class in c# to create, copy, rename and delete directories and subdirectories based on our requirements.