How to use the directory class in C#?

CsharpProgrammingServer Side Programming

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);
      }
   }
}
raja
Published on 18-Sep-2018 12:58:23
Advertisements