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:

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

C# DirectoryInfo Properties:

PropertyUses
AttributesTo retrieve or to set the attributes for the current file or directory.
CreationTimeTo retrieve or to set the creation time of the current file or directory.
CreationTimeUtcTo retrieve or to set the creation time, in coordinated universal time (UTC).
ExistsTo retrieve a value determining whether the directory exists.
ExtensionTo retrieve the string representing the extension part of the file.
FullNameTo retrieve the full path of the directory.
LastAccessTimeTo retrieve or to set the time the current file or directory was last accessed.
LastAccessTimeUtcTo retrieve or to set the time, in coordinated universal time (UTC) when the current file or directory was last accessed.
LastWriteTimeTo retrieve or to set the time when the current file or directory was last written.
LastWriteTimeUtcTo retrieve or to set the time, in coordinated universal time (UTC), when the current file or directory was last written.
NameTo retrieve the name of this DirectoryInfo instance.
ParentTo retrieve the parent directory of a specified subdirectory.
RootTo retrieve the root portion of the directory.

C# DirectoryInfo Methods:

MethodUses
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.

Content Protection by DMCA.com
Please Share