Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read all text files in a folder with StreamReader

Tags:

c#

I am trying to read all .txt files in a folder using stream reader. I have this now and it works fine for one file but I need to read all files in the folder. This is what I have so far. Any suggestions would be greatly appreciated.

using (var reader = new StreamReader(File.OpenRead(@"C:\ftp\inbox\test.txt")))
like image 414
robert woods Avatar asked Feb 11 '26 01:02

robert woods


1 Answers

You can use Directory.EnumerateFiles() method instead of.

Returns an enumerable collection of file names that match a search pattern in a specified path.

var txtFiles = Directory.EnumerateFiles(sourceDirectory, "*.txt");
foreach (string currentFile in txtFiles)
{
   ...
}
like image 151
Soner Gönül Avatar answered Feb 12 '26 15:02

Soner Gönül



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!