C# DirectoryInfo Class2 min read

In C#, DirectoryInfo Class is a part of System.IO namespace and it provides properties and methods to perform various operations on directory and subdirectory such as to create, delete and move directory. It cannot be inherited.

C# DirectoryInfo Properties

The following tables list the properties provided by DirectoryInfo class.

PropertyDescription
CreationTimeIt is used to get or set the creation time of the current file or directory.
ExistsThis property provides a value indicating whether the directory exists or not.
ExtensionIt is used to get the extension part of the file.
NameIt is used to get the name of this DirectoryInfo instance.
FullNameThis property provides the full path of a directory.
LastAccessTimeIt is used to gets or sets the time the current file or directory was last accessed.
ParentIt is useful to get the parent directory of a specified subdirectory.

C# DirectoryInfo Methods

MethodDescription
Create()It creates a directory.
Delete()It deletes the specified directory.
EnumerateDirectories()It returns an enumerable collection of directory information in the current directory.
GetDirectories()Returns the subdirectories of the current directory.
GetFiles()Returns the file list from the current directory.
MoveTo()It moves a specified directory and its contents to a new path.
ToString()It returns the original path that was passed by the user.

Example: C# program for DirectoryInfo Class

The program is used to create a directory in C#.

Output:

Directory Created Successfully!

A directory named “Sample” will be created on a path that you will specify in a program. The program also checks if the directory specified already exists in that path or not.


Example: C# program for DirectoryInfo Class tp delete a directory

Output:

Directory Deleted Successfully!


MORE

Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a numberĀ using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …