The DirectoryInfo class is part of the namespace. It is used to create, delete and move directories. It provides a way to perform operations related to directories and subdirectories. This is a sealed class, so it cannot be inherited.
The DirectoryInfo class provides the constructors, methods and properties listed below.
C# DirectoryInfo Syntax
[SerializableAttribute] [ComVisibleAttribute(true)] public sealed class DirectoryInfo : FileSystemInfo
C# DirectoryInfo constructor
The following table lists the constructors of the DirectoryInfo class.
Constructor | describe |
---|---|
DirectoryInfo(String) | It is used to initialize a new instance of the DirectoryInfo class on the specified path. |
C# DirectoryInfo Properties
The properties of the DirectoryInfo class are listed in the following table.
property | describe |
---|---|
Attributes | It is used to get or set properties of 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 the creation time in coordinated world time (UTC). |
Exists | It is used to get a value indicating whether the directory exists. |
Extension | It is used to get a string representing the file extension part. |
FullName | It is used to get the full path to the directory. |
LastAccessTime | It is used to get or set the time when the current file or directory was last accessed. |
LastAccessTimeUtc | Used to get or set the time in the coordinated world time (UTC) that the current file or directory was last accessed. |
LastWriteTime | It is used to get or set the last time the current file or directory is 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 the specified subdirectory. |
Root | It is used to get the root part of the directory. |
C# DirectoryInfo method
The following table lists the methods defined in the DirectoryInfo class.
method | describe |
---|---|
Create() | It is used to create a directory. |
Create(DirectorySecurity) | It is used to create directories using DirectorySecurity object. |
CreateObjRef(Type) | It is used to create an object that contains all the relevant information needed to generate a proxy for communicating with the remote object. |
CreateSubdirectory(String) | It is used to create a subdirectory or subdirectory on the specified path. |
CreateSubdirectory(String,DirectorySecurity) | It is used to create subdirectories or subdirectories on a specified path with specified security. |
Delete() | If DirectoryInfo is empty, it is used to delete DirectoryInfo. |
Delete(Boolean) | It is used to delete instances of DirectoryInfo, specifying whether to delete subdirectories and files. |
EnumerateDirectories() | It returns the available directory information collection in the current directory. |
EnumerateFiles() | It returns countless collections of file information in the current directory. |
GetAccessControl() | It is used to obtain a DirectorySecurity object that encapsulates the directory's access control list (ACL) entry. |
GetDirectories() | It returns the subdirectory of the current directory. |
GetFiles() | It returns a list of files from the current directory. |
GetType() | It is used to get the type of the current instance. |
MoveTo(String) | It is used to move DirectoryInfo instance and its contents to a new path. |
Refresh() | It is used to refresh the state of an object. |
SetAccessControl(DirectorySecurity) | It is used to set the access control list (ACL) entry for the DirectorySecurity object description. |
ToString() | It returns the original path passed by the user. |
C# DirectoryInfo Example
In the following example, create a directory named yiibai_dir by specifying the directory path. Refer to the following example code implementation -
using System; using ; namespace CSharpProgram { class Program { static void Main(string[] args) { // Provide directory name with complete location. DirectoryInfo directory = new DirectoryInfo(@"F:\worksp\csharp\yiibai_dir"); try { // Check, directory exist or not. if () { ("Directory already exist."); return; } // Creating a new directory. (); ("The directory is created successfully."); } catch (Exception e) { ("Directory not created: {0}", ()); } } } }
Execute the above example code and get the following results -
The directory is created successfully.
Open the directory: F:\worksp\csharp You should see a directory named: yiibai_dir.
The DirectoryInfo class also provides a method to delete the created directory.
C# DirectoryInfo Example: Delete Directory
In the following program, we will delete the yiibai_dir directory created in the above example program.
using System; using ; namespace CSharpProgram { class Program { static void Main(string[] args) { // Providing directory name with complete location. DirectoryInfo directory = new DirectoryInfo(@"F:\worksp\csharp\yiibai_dir"); try { // Deleting directory (); ("The directory is deleted successfully."); } catch (Exception e) { ("Something went wrong: {0}", ()); } } } }
Execute the above example code and get the following results -
The directory is deleted successfully.
Open the directory: F:\worksp\csharp You should see that the yiibai_dir directory has been deleted.
If the directory does not exist at the specified location, it will throw an exception. You can modify the above code and test it yourself.
This is the end of this article about the detailed explanation of the usage of C#DirectoryInfo class. For more related content of C#DirectoryInfo class, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!