HI WELCOME TO SIRIS

C# DirectoryInfo Class

Leave a Comment
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.

C# DirectoryInfo Syntax

  1. [SerializableAttribute]  
  2. [ComVisibleAttribute(true)]  
  3. public sealed class DirectoryInfo : FileSystemInfo  

C# DirectoryInfo Constructors

The following table contains the constructors for the DirectoryInfo class.
ConstructorDescription
DirectoryInfo(String)It is used to initialize a new instance of the DirectoryInfo class on the specified path.

C# DirectoryInfo Properties

The following table contains the properties of the DirectoryInfo class.
PropertyDescription
AttributesIt is used to get or set the attributes for the current file or directory.
CreationTimeIt is used to get or set the creation time of the current file or directory.
CreationTimeUtcIt is used to get or set creation time, in coordinated universal time (UTC).
ExistsIt is used to get a value indicating whether the directory exists.
ExtensionIt is used to get the string representing the extension part of the file.
FullNameIt is used to get the full path of the directory.
LastAccessTimeIt is used to get or set the time the current file or directory was last accessed.
LastAccessTimeUtcIt is used to get or set the time, in coordinated universal time (UTC) that the current file or directory was last accessed.
LastWriteTimeIt is used to get or set the time when the current file or directory was last written.
LastWriteTimeUtcIt is used to get or set the time, in coordinated universal time (UTC), when the current file or directory was last written.
NameIt is used to get the name of this DirectoryInfo instance.
ParentIt is used to get the parent directory of a specified subdirectory.
RootIt is used to get the root portion of the directory.

C# DirectoryInfo Methods

The following table contains the methods of the DirectoryInfo class.
MethodDescription
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 the following example, we are creating a javatpoint directory by specifying the directory path.
  1. using System;  
  2. using System.IO;  
  3. namespace CSharpProgram  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             // Provide directory name with complete location.  
  10.             DirectoryInfo directory = new DirectoryInfo(@"F:\javatpoint");  
  11.             try  
  12.             {  
  13.                 // Check, directory exist or not.  
  14.                 if (directory.Exists)  
  15.                 {  
  16.                     Console.WriteLine("Directory already exist.");  
  17.                     return;  
  18.                 }  
  19.                 // Creating a new directory.  
  20.                 directory.Create();  
  21.                 Console.WriteLine("The directory is created successfully.");  
  22.             }  
  23.             catch (Exception e)  
  24.             {  
  25.                 Console.WriteLine("Directory not created: {0}", e.ToString());  
  26.             }  
  27.         }  
  28.     }  
  29. }  
Output:
The directory is created successfully.
In below screenshot, we can see that a directory is created.
CSharp Directory info 1
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.

C# DirectoryInfo Example: Deleting Directory

  1. using System;  
  2. using System.IO;  
  3. namespace CSharpProgram  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             // Providing directory name with complete location.  
  10.             DirectoryInfo directory = new DirectoryInfo(@"F:\javatpoint");  
  11.             try  
  12.             {  
  13.                 // Deleting directory  
  14.                 directory.Delete();  
  15.                 Console.WriteLine("The directory is deleted successfully.");  
  16.             }  
  17.             catch (Exception e)  
  18.             {  
  19.                 Console.WriteLine("Something went wrong: {0}", e.ToString());  
  20.             }  
  21.         }  
  22.     }  
  23. }  
Output:
The directory is deleted successfully.
It throws a System.IO.DirectoryNotFoundException exception if the specified directory not present at the location.

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.