← Back to C# Course | Chapter 14: File I/O & Streams | Lesson 3 of 6

Directory

In this page:

  1. Directory

Directory

The Directory class provides static methods for creating, checking, listing, and deleting folders, such as Directory.CreateDirectory and Directory.Exists. Directory.GetFiles returns the files inside a folder as an array of paths. It's the folder-level counterpart to the file-level File class.

Note: Directory.CreateDirectory does nothing (and doesn't throw) if the directory already exists.

Example: Directory

markup
using System;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        string folder = "reports";
        Directory.CreateDirectory(folder);

        Console.WriteLine(Directory.Exists(folder));

        File.WriteAllText(Path.Combine(folder, "a.txt"), "data");
        string[] files = Directory.GetFiles(folder);

        foreach (string f in files)
        {
            Console.WriteLine(f);
        }
    }
}
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.