To get a list of file names in a specific folder, use the Directory.GetFiles method. This method allows you to filter files using “wildcards” for specific extensions or naming patterns. You can also search through subfolders by specifying search options.
目次
Table of Contents
- Wildcard Characters and Meanings
- Specifying SearchOption (Search Scope)
- Implementation Sample: Searching for Log Files
- Explanation
Wildcard Characters and Meanings
These are special characters used in the search pattern (searchPattern) for file searches.
| Wildcard | Meaning | Usage Example | Matching Examples | Non-matching Examples |
| * (Asterisk) | Zero or more characters | *.txt | memo.txt, .txt, abc.txt | memo.log |
| ? (Question mark) | Exactly one character | data_?.csv | data_1.csv, data_A.csv | data_10.csv, data.csv |
Specifying SearchOption (Search Scope)
This enum specifies whether to search inside subfolders.
| Value | Meaning | Description |
| TopDirectoryOnly | Top-level directory only | Searches only the files directly inside the specified folder (Default). |
| AllDirectories | All directories | Recursively searches the specified folder and all its subfolders. |
Implementation Sample: Searching for Log Files
The following code creates a LogData folder, places some dummy files inside, and then searches for and displays files using different patterns and options.
Sample Code
using System;
using System.IO;
public class Program
{
public static void Main()
{
// 1. Set up test environment (Create directories and dummy files)
string rootDir = "LogData";
string subDir = Path.Combine(rootDir, "Backup");
// Create folders
Directory.CreateDirectory(subDir);
// Create files
// Pattern 1: log_xxxx.txt directly under root
File.WriteAllText(Path.Combine(rootDir, "log_2023.txt"), "log");
File.WriteAllText(Path.Combine(rootDir, "log_2024.txt"), "log");
File.WriteAllText(Path.Combine(rootDir, "error.log"), "error"); // Different extension
// Pattern 2: File inside subfolder
File.WriteAllText(Path.Combine(subDir, "log_2020.txt"), "old log");
Console.WriteLine($"--- Target Directory: {rootDir} ---\n");
// ---------------------------------------------------------
// Case 1: Wildcard "*" and top-level search
// ---------------------------------------------------------
// "*.txt" -> All files with .txt extension
Console.WriteLine("[1] *.txt (Top directory only):");
string[] txtFiles = Directory.GetFiles(rootDir, "*.txt", SearchOption.TopDirectoryOnly);
foreach (var file in txtFiles)
{
Console.WriteLine($" Found: {Path.GetFileName(file)}");
}
// ---------------------------------------------------------
// Case 2: Wildcard "?" and recursive search
// ---------------------------------------------------------
// "log_????.txt" -> .txt files with exactly 4 characters after "log_"
// SearchOption.AllDirectories -> Includes the subfolder (Backup)
Console.WriteLine("\n[2] log_????.txt (Including subfolders):");
string[] logFiles = Directory.GetFiles(rootDir, "log_????.txt", SearchOption.AllDirectories);
foreach (var file in logFiles)
{
// Display only the file name as the path can be long
Console.WriteLine($" Found: {Path.GetFileName(file)}");
}
}
}
Execution Result
--- Target Directory: LogData ---
[1] *.txt (Top directory only):
Found: log_2023.txt
Found: log_2024.txt
[2] log_????.txt (Including subfolders):
Found: log_2023.txt
Found: log_2024.txt
Found: log_2020.txt
Explanation
- Return Value:
Directory.GetFilesreturns a string array (string[]) containing the full paths (or relative paths) of the files found. - Behavior of
?: As seen in Result [2],error.logis excluded because it does not match the pattern, whilelog_2020.txtin the subfolder is detected thanks toAllDirectories. - Important Note: If a folder contains tens of thousands of files, using
GetFilescan be inefficient because it does not return control until the entire array is created. In such cases, consider usingDirectory.EnumerateFilesfor better memory efficiency.
