Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find a file with a certain extension in folder

Tags:

c#

filepath

Given a folder path (like C:\Random Folder), how can I find a file in it that holds a certain extension, like txt? I assume I'll have to do a search for *.txt in the directory, but I'm not sure how I'm supposed to start this search in the first place.

like image 819
Dominic K Avatar asked Jun 30 '10 18:06

Dominic K


People also ask

How do I find hidden extensions for known file types?

Open Control Panel > Appearance and Personalization. Now, click on Folder Options or File Explorer Option, as it is now called. Select the View tab. In this tab, under Advanced Settings, you will see the option Hide extensions for known file types.


2 Answers

Look at the System.IO.Directory class and the static method GetFiles. It has an overload that accepts a path and a search pattern. Example:

 string[] files = System.IO.Directory.GetFiles(path, "*.txt"); 
like image 152
Anthony Pegram Avatar answered Sep 20 '22 03:09

Anthony Pegram


You could use the Directory class

 Directory.GetFiles(path, "*.txt", SearchOption.AllDirectories) 
like image 21
sgriffinusa Avatar answered Sep 20 '22 03:09

sgriffinusa