C Sharp DirectoryInfo Class

C# DirectoryInfo Class

Being a part of the System.IO namespace, the DirectoryInfo class in C# facilitates various constructors, methods, and properties to perform operations related to directory and subdirectory, i.e., to create, delete and move a directory or subdirectory. The DirectoryInfo class in C# cannot be inherited, because it is a sealed class.

C# DirectoryInfo Syntax:

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

C# DirectoryInfo Constructors:

Constructor Uses
DirectoryInfo(String) For initializing a new instance of the DirectoryInfo class on the specified path.

C# DirectoryInfo Properties:

Property Uses
Attributes To retrieve or to set the attributes for the current file or directory.
CreationTime To retrieve or to set the creation time of the current file or directory.
CreationTimeUtc To retrieve or to set the creation time, in coordinated universal time (UTC).
Exists To retrieve a value determining whether the directory exists.
Extension To retrieve the string representing the extension part of the file.
FullName To retrieve the full path of the directory.
LastAccessTime To retrieve or to set the time the current file or directory was last accessed.
LastAccessTimeUtc To retrieve or to set the time, in coordinated universal time (UTC) when the current file or directory was last accessed.
LastWriteTime To retrieve or to set the time when the current file or directory was last written.
LastWriteTimeUtc To retrieve or to set the time, in coordinated universal time (UTC), when the current file or directory was last written.
Name To retrieve the name of this DirectoryInfo instance.
Parent To retrieve the parent directory of a specified subdirectory.
Root To retrieve the root portion of the directory.

C# DirectoryInfo Methods:

Method Uses
Create() For creating a directory.
Create(DirectorySecurity) For creating a directory using a DirectorySecurity object.
CreateObjRef(Type) For creating an object to include all the relevant information which is needed to generate a proxy to communicate with a remote object.
CreateSubdirectory(String) For creating a subdirectory or subdirectories on the specified path.
CreateSubdirectory(String,DirectorySecurity) For creating a subdirectory or subdirectories on the specified path with the specified security.
Delete() For eliminating this DirectoryInfo if it is empty.
Delete(Boolean) For eliminating this instance of a DirectoryInfo, determining whether to delete subdirectories and files.
EnumerateDirectories() For retrieving an enumerable collection of directory information in the current directory.
EnumerateFiles() For retrieving an enumerable collection of file information in the current directory.
GetAccessControl() For retrieving a DirectorySecurity object to encapsulate the access control list (ACL) entries for the directory.
GetDirectories() For retrieving the subdirectories of the current directory.
GetFiles() For retrieving a file list from the current directory.
GetType() For retrieving the Type of the current instance.
MoveTo(String) For relocating a DirectoryInfo instance and its contents to a new path.
Refresh() To refresh the state of the object.
SetAccessControl(DirectorySecurity) To set the access control list (ACL) entries defined by a DirectorySecurity object.
ToString() To retrieve the original path, passed by the user.

Example:

using System;  
using System.IO;  
namespace Example  
{  
    class Content  
    {  
        static void Main(string[] args)  
        {  
            // Provide directory name with complete location.  
            DirectoryInfo dir = new DirectoryInfo(@"D:\csharp");  
            try  
            {  
                // Check, directory exist or not.  
                if (dir.Exists)  
                {  
                    Console.WriteLine("Already Existing Directory..");  
                    return;  
                }  
                // Creating a new directory.  
                dir.Create();  
                Console.WriteLine("Success!!");  
            }  
            catch (Exception e)  
            {  
                Console.WriteLine("Failure: {0}", e.ToString());  
            }  
        }  
    }  
}

Output:

Explanation:

In the above example, we are using the C# DirectoryInfo Class for creating a directory. Here, we are creating a “Program” directory. The directory path needs to be specified for this.

Example:

using System;  
using System.IO;  
namespace Example  
{  
    class Content  
    {  
        static void Main(string[] args)  
        {  
            // Providing directory name with complete location.  
            DirectoryInfo dir = new DirectoryInfo(@"D:\csharp");  
            try  
            {  
                // Deleting directory  
                dir.Delete();  
                Console.WriteLine("Deleted successfully!!");  
            }  
            catch (Exception e)  
            {  
                Console.WriteLine("Failure: {0}", e.ToString());  
            }  
        }  
    }  
}

Output:

Explanation:

In the above example, we are using the C# DirectoryInfo Class for deleting an already created directory. In case, the specified directory is not present at the specified location, a System.IO.DirectoryNotFoundException exception will be displayed.

Please follow and like us:
Content Protection by DMCA.com