---
title: "How can you list all files in a directory, including subdirectories?"  
description: "How can you list all files in a directory, including subdirectories?"  
author: "ICSM Computer"  
published: 2025-05-06  
updated: 2025-05-06  
canonical: https://www.mindstick.com/interview/34090/how-can-you-list-all-files-in-a-directory-including-subdirectories  
category: "c#"  
tags: ["c#", "file handling"]  
reading_time: 2 minutes  

---

# How can you list all files in a directory, including subdirectories?

To **list all files in a directory, including its subdirectories**, you can use the `Directory.GetFiles` method with the `SearchOption.AllDirectories` option in C#.

### Example: Get all files recursively

```cs
using System;
using System.IO;

class Program
{
    static void Main()
    {
        string rootPath = @"C:\MyFolder";

        try
        {
            string[] allFiles = Directory.GetFiles(rootPath, "*.*", SearchOption.AllDirectories);

            foreach (string file in allFiles)
            {
                Console.WriteLine(file);
            }
        }
        catch (UnauthorizedAccessException ex)
        {
            Console.WriteLine("Access denied: " + ex.Message);
        }
        catch (DirectoryNotFoundException ex)
        {
            Console.WriteLine("Directory not found: " + ex.Message);
        }
    }
}
```

### Parameters:

1. `*.*` – Matches all files.
2. `SearchOption.AllDirectories` – Tells the method to search recursively.
3. You can filter by extension too, e.g., `"*.txt"`.

### Notes:

1. Wrap in a `try-catch` block to handle access issues.
2. Be cautious with very large directory trees—it can be slow or memory-intensive.

## Answers

### Answer by ICSM Computer

To **list all files in a directory, including its subdirectories**, you can use the `Directory.GetFiles` method with the `SearchOption.AllDirectories` option in C#.

### Example: Get all files recursively

```cs
using System;
using System.IO;

class Program
{
    static void Main()
    {
        string rootPath = @"C:\MyFolder";

        try
        {
            string[] allFiles = Directory.GetFiles(rootPath, "*.*", SearchOption.AllDirectories);

            foreach (string file in allFiles)
            {
                Console.WriteLine(file);
            }
        }
        catch (UnauthorizedAccessException ex)
        {
            Console.WriteLine("Access denied: " + ex.Message);
        }
        catch (DirectoryNotFoundException ex)
        {
            Console.WriteLine("Directory not found: " + ex.Message);
        }
    }
}
```

### Parameters:

1. `*.*` – Matches all files.
2. `SearchOption.AllDirectories` – Tells the method to search recursively.
3. You can filter by extension too, e.g., `"*.txt"`.

### Notes:

1. Wrap in a `try-catch` block to handle access issues.
2. Be cautious with very large directory trees—it can be slow or memory-intensive.


---

Original Source: https://www.mindstick.com/interview/34090/how-can-you-list-all-files-in-a-directory-including-subdirectories

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
