C# DirectoryInfo Class - DirectoryInfo Class in C# with Examples



C# DirectoryInfo Class - DirectoryInfo Class in C# with Examples

  • Directory Info class is a part of System.IO namespace and it is used to create, delete and move directory.
  • It provides methods to perform operations related to directory and subdirectory and we cannot inherit it because it is a sealed class.

Syntax

[SerializableAttribute]  
[ComVisibleAttribute(true)]  
public sealed class DirectoryInfo : FileSystemInfo  
 

Directory Info Constructors

Constructor Description
Directory Info (String) It is used to initialize a new instance of the Directory Info class on the specified path.

Directory Info Methods

Methods Description
Create () It is used to create a directory.
Create (Directory Security) It is used to create a directory using a Directory Security object.
Create Obj Ref (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.
Create Sub directory (String) It is used to create a subdirectory or subdirectories on the specified path.
Create Subdirectory (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 Directory Info if it is empty.
Delete (Boolean) It is used to delete this instance of a Directory Info, specifying whether to delete subdirectories and files.
Enumerate Directories () It returns an enumerable collection of directory information in the current directory.
Enumerate Files () It returns an enumerable collection of file information in the current directory.
Get Access Control () It is used to get a Directory Security object that encapsulates the access control list (ACL) entries for the directory.
Get Directories () It returns the subdirectories of the current directory.
Get Files () It returns a file list from the current directory.
Get Type () It is used to get the Type of the current instance.
Move To (String) It is used to move a Directory Info instance and its contents to a new path.
Refresh () It is used to refresh the state of the object.
Set Access Control (Directory Security) It is used to set access control list (ACL) entries described by a Directory Security object.
To String () It returns the original path that was passed by the user.

Directory Info Properties

Properties Description
Attributes It is used to get or set the attributes for the current file or directory.
Creation Time It is used to get or set the creation time of the current file or directory.
Creation Time Utc 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.
Full Name It is used to get the full path of the directory.
Last Access Time It is used to get or set the time the current file or directory was last accessed.
Last Access Time Utc It is used to get or set the time, in coordinated universal time (UTC) that the current file or directory was last accessed.
Last Write Time It is used to get or set the time when the current file or directory was last written.
Last Write Time Utc 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 Directory Info 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.

Sample Code

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:\Wikitechy");  
            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());  
            }  
        }  
    }  
}  

Output

C# DirectoryInfo

Related Searches to C# DirectoryInfo Class - DirectoryInfo Class in C# with Examples