find and solve || findandsolve.com
Please wait.....

C# DirectoryInfo Class

In the C#, DirectoryInfo provides properties and instance methods for the create, delete, copy, move, and opening of files, and aids in the creation of FileStream objects. Which class is a part of System.IO namespace.

DirectoryInfo class is a sealed class so, we cannot inherit it.

C# DirectoryInfo Syntax

[SerializableAttribute]  

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

C# DirectoryInfo Constructors

The given below table contains below the constructors for the DirectoryInfo class.

ConstructorDescription
DirectoryInfo(String) Initialize a new instance of the DirectoryInfo class on the specified path.

C# DirectoryInfo Properties

The given table contains below the properties of the DirectoryInfo class.

PropertyDescription
AttributesGet or set the attributes for the current file or directory.
CreationTimeGet or set the creation time of the current file or directory.
CreationTimeUtc Get or set creation time, in coordinated universal time (UTC).
ExistsGet a value indicating whether the directory exists.
ExtensionGet the string representing the extension part of the file.
FullNameGet the full path of the directory.
LastAccessTimeGet or set the time the current file or directory was last accessed.
LastAccessTimeUtcGet or set the time, in coordinated universal time (UTC) that the current file or directory was last accessed.
LastWriteTimeGet or set the time when the current file or directory was last written.
LastWriteTimeUtcGet or set the time, in coordinated universal time (UTC), when the current file or directory was last written.
NameGet the name of this DirectoryInfo instance.
ParentGet the parent directory of a specified subdirectory.
RootGet the root portion of the directory.

C# DirectoryInfo Methods

The given table contains below the methods of the DirectoryInfo class.

MethodDescription
Create() Create a directory.
Create(DirectorySecurity) Create a directory using a DirectorySecurity object.
CreateObjRef(Type) Create an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.
CreateSubdirectory(String) Create a subdirectory or subdirectories on the specified path.
CreateSubdirectory(String,DirectorySecurity) Create a subdirectory or subdirectories on the specified path with the specified security.
Delete() Delete this DirectoryInfo if it is empty.
Delete(Boolean) Delete this instance of a DirectoryInfo, specifying whether to delete subdirectories and files.
EnumerateDirectories() Returns an enumerable collection of directory information in the current directory.
EnumerateFiles() Returns an enumerable collection of file information in the current directory.
GetAccessControl() Get a DirectorySecurity object that encapsulates the access control list (ACL) entries for the directory.
GetDirectories() Returns the subdirectories of the current directory.
GetFiles() Returns a file list from the current directory.
GetType() Get the Type of the current instance.
MoveTo(String) Move a DirectoryInfo instance and its contents to a new path.
Refresh() Refresh the state of the object.
SetAccessControl(DirectorySecurity) Set access control list (ACL) entries described by a DirectorySecurity object.
ToString() Returns the original path that was passed by the user.

C# DirectoryInfo Example

In the given below example, we are creating a FindAndSolve directory by specifying the directory path.

using System;  
using System.IO;
namespace CSharpProgram
{
   public class Program
    {
       public static void Main(string[] args)         {             // Provide directory name with complete location.
            DirectoryInfo directory = new DirectoryInfo(@"C:\FindAndSolve");
            try
            {
                //Check, directory exist or not.
                if (directory.Exists)
                {
                    Console.WriteLine("Directory already exist.");
                    return;
                }
                //Creating a new directory.
                directory.Create();
                Console.WriteLine("TYour request has been successful.");
            }
            catch (Exception e)
            {
                Console.WriteLine("Directory not created: {0}", e.ToString());
            }         }
    }
}  

Output

Your request has been successful.

In the below screenshot, we can see that a directory is created.


C# DirectoryInfo Example: Deleting Directory

using System;  
using System.IO;
namespace CSharpProgram
{
    pubic class Program
    {
        public static void Main(string[] args)
        {
            //Providing directory name with complete location.
            DirectoryInfo directory = new DirectoryInfo(@"C:\FindAndSolve");
            try
            {
                // Deleting directory
                directory.Delete();
                Console.WriteLine("The directory is deleted successfully.");
            }
            catch (Exception e)
            {
                Console.WriteLine("Something went wrong: {0}", e.ToString());
            }         }
    }
}

Output

The directory is deleted successfully.