TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# DirectoryInfo

C# DirectoryInfo for beginners and professionals with examples on overloading, method overriding, inheritance, aggregation, base, polymorphism, sealed, abstract, interface, namespaces, exception handling, file io, collections, multithreading, reflection etc.

<< Back to C

C# DirectoryInfo Class

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

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

C# DirectoryInfo Constructors

The following table contains the constructors for the DirectoryInfo class.

Constructor Description
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.

Property Description
Attributes It is used to get or set the attributes for the current file or directory.
CreationTime It is used to get or set the creation time of the current file or directory.
CreationTimeUtc 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.
FullName It is used to get the full path of the directory.
LastAccessTime It is used to get or set the time the current file or directory was last accessed.
LastAccessTimeUtc It is used to get or set the time, in coordinated universal time (UTC) that the current file or directory was last accessed.
LastWriteTime It is used to get or set the time when the current file or directory was last written.
LastWriteTimeUtc 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 DirectoryInfo 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.

C# DirectoryInfo Methods

The following table contains the methods of the DirectoryInfo class.

Method Description
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 TheDeveloperBlog directory by specifying the directory path.

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:\TheDeveloperBlog");
            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:

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

using System;
using System.IO;
namespace CSharpProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            // Providing directory name with complete location.
            DirectoryInfo directory = new DirectoryInfo(@"F:\TheDeveloperBlog");
            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.

It throws a System.IO.DirectoryNotFoundException exception if the specified directory not present at the location.


Next TopicC# Serialization




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf