How to use the directory class in C#?


The Directory class in C# is used to manipulate the directory structure. It has methods to create, move, remove directories.

The following are some of the methods of the Directory class.

Sr.No.Method & Description
1CreateDirectory(String)
Creates all directories and subdirectories in the specified path
2Delete(String)
Deletes an empty directory
3Exists(String)
Whether the given path refers to an existing directory
4GetCreationTime(String)
Gets the creation date and time of a directory.
5GetCurrentDirectory()
Gets the current working directory
6GetFiles(String)

Let us learn about the usage of GetFiles() method in Directory class. It displays all the files in the specified directory.

Example

using System;
using System.IO;
class Program {
   static void Main() {
      // Get all files in the D directory
      string[] arr = Directory.GetFiles(@"D:\");
      Console.WriteLine("Files:");
      foreach (string n in arr) {
         Console.WriteLine(n);
      }
   }
}

Updated on: 23-Jun-2020

221 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements