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

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More

Inverted Half Pyramid Pattern of Alphabets in C

In this tutorial, we will write a C program to print half Pyramid using alphabets/characters. Before that, you may go through the following topic in …
Read More