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

Keith Number Program in C

In this tutorial, we will write a Keith number in C. It is one of the most asked questions in an interview. Before that, you …
Read More

Keith Number Program in C++

In this tutorial, we will write a Keith number in C++. It is one of the most asked questions in an interview. Before that, you …
Read More

Singly linked list Program C++

In this tutorial, we will write a C++ program to implement singly linked list. In this program, we will create a separate function for each …
Read More

Fascinating Number Program in C

In this tutorial, we will write a C program to check whether the given number is a Fascinating number or not. You may go through …
Read More

Peterson Number in C

In this tutorial, we will learn about the Peterson number and check if the number is Peterson or not in C. What is Peterson Number? …
Read More

Peterson Number in C++

In this tutorial, we will learn about the Peterson number and write a C++ program to check if the given number is a Peterson number …
Read More