---
title: "How do you list only files with a specific extension (e.g., .txt) in a directory?"  
description: "How do you list only files with a specific extension (e.g., .txt) in a directory?"  
author: "Ravi Vishwakarma"  
published: 2025-05-08  
updated: 2025-05-08  
canonical: https://www.mindstick.com/interview/34099/how-do-you-list-only-files-with-a-specific-extension-e-g-txt-in-a-directory  
category: "c#"  
tags: ["c#", "file handling"]  
reading_time: 2 minutes  

---

# How do you list only files with a specific extension (e.g., .txt) in a directory?

To **list only files with a specific extension** (e.g., `.txt`) in a directory in C#, you can use the `Directory.GetFiles` method with a search pattern.

### Example: Get `.txt` files from a folder

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

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

        try
        {
            string[] txtFiles = Directory.GetFiles(folderPath, "*.txt");

            foreach (string file in txtFiles)
            {
                Console.WriteLine(file);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
        }
    }
}
```

### To include subdirectories too:

Use the `SearchOption.AllDirectories` flag:

```cs
string[] txtFiles = Directory.GetFiles(folderPath, "*.txt", SearchOption.AllDirectories);
```

### Notes:

1. Search pattern like `"*.txt"` can be `"*.csv"`, `"data*.json"`, etc.
2. It returns **full file paths**.
3. You can also filter manually using `Directory.EnumerateFiles()` with `LINQ` for more complex logic.

## Answers

### Answer by Ravi Vishwakarma

To **list only files with a specific extension** (e.g., `.txt`) in a directory in C#, you can use the `Directory.GetFiles` method with a search pattern.

### Example: Get `.txt` files from a folder

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

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

        try
        {
            string[] txtFiles = Directory.GetFiles(folderPath, "*.txt");

            foreach (string file in txtFiles)
            {
                Console.WriteLine(file);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
        }
    }
}
```

### To include subdirectories too:

Use the `SearchOption.AllDirectories` flag:

```cs
string[] txtFiles = Directory.GetFiles(folderPath, "*.txt", SearchOption.AllDirectories);
```

### Notes:

1. Search pattern like `"*.txt"` can be `"*.csv"`, `"data*.json"`, etc.
2. It returns **full file paths**.
3. You can also filter manually using `Directory.EnumerateFiles()` with `LINQ` for more complex logic.


---

Original Source: https://www.mindstick.com/interview/34099/how-do-you-list-only-files-with-a-specific-extension-e-g-txt-in-a-directory

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
