0

Hello,
I have written a C# console application that currently takes a data file as input, processes the data and writes out 2 new data files. I would like for it to be able to process ALL the files in a given directory. How might I do this? Thanks!

3
Contributors
3
Replies
5
Views
6 Years
Discussion Span
Last Post by hotdang
1

You can get a list of files in a directory by using the function Directory.GetFiles();

Here is an example that reads all the data from each file and stores it in the variable 'data'.

string directory = @"C:\";
foreach (string file in Directory.GetFiles(directory))
{
    string data = "";
    using (StreamReader sr = new StreamReader(file))
    {
        data = sr.ReadToEnd();
    }

    // Do something with the data from each file found
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.