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
1 CreateDirectory(String)
Creates all directories and subdirectories in the specified path
2 Delete(String)
Deletes an empty directory
3 Exists(String)
Whether the given path refers to an existing directory
4 GetCreationTime(String)
Gets the creation date and time of a directory.
5 GetCurrentDirectory()
Gets the current working directory
6 GetFiles(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: 2020-06-23T12:33:42+05:30

419 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements