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 To include subdirectories too: Use the SearchOption.AllDirectories flag: Notes: Search pattern like \"*.txt\" can be \"*.csv\" , \"data*.json\" , etc. It returns full file paths . You can also filter manually using Directory.EnumerateFiles() with LINQ for more complex logic.
To list only files with a specific extension (e.g.,
.txt) in a directory in C#, you can use theDirectory.GetFilesmethod with a search pattern.Example: Get
.txtfiles from a folderTo include subdirectories too:
Use the
SearchOption.AllDirectoriesflag:Notes:
"*.txt"can be"*.csv","data*.json", etc.Directory.EnumerateFiles()withLINQfor more complex logic.