I want to access all the files of any type(any extension) from a specific folder and I want to read all the data in from these files.
Asked
Active
Viewed 112 times
-1
-
2Hi Awais, welcome to stackoverflow. kindly update your question with what you have tried so far, what issue you faced, specific code sample input and expected output. Also read [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example), it will help you to frame your question in proper way – Prasad Telkikar Jun 15 '19 at 12:17
1 Answers
0
You should start with learning about DirectoryInfo (read directory structure and enumerate files) and FileInfo classes. Then you might want to use FileStream class in order to read files' contents. Here's my simple example to help you get started:
var dirInfo = new DirectoryInfo(@"c:\temp"); // Obtaining DirectoryInfo object for specified path
foreach (var file in dirInfo.EnumerateFiles()) // Enumerating files, file is FileInfo object
{
Console.WriteLine($"Reading {file.Name}...");
var fileStream = file.OpenRead(); // obtainig FileStream to read from a file
var bytesArray = new byte[fileStream.Length]; // allocating array to read into
fileStream.Read(bytesArray);
// Do some stuff with the data
Console.WriteLine(@"Read " + bytesArray.Length + " bytes");
}
Please bear in mind that you need to add using in order to use System.IO namespace:
using System.IO;

Stas Ivanov
- 1,173
- 1
- 14
- 24