JayPav
JayPav

Reputation: 13

How can I read the first line from every text file in a specific directory and add each line to a listbox in a C# WPF app?

I have a directory that Contains x amount of text files. The first Line on each text file needs to be added to a list box in a WPF application when the application starts. How can I read the first line from every text file and add each line to my list box?

Upvotes: 1

Views: 333

Answers (1)

Bruno Garcia
Bruno Garcia

Reputation: 6398

Something like this should do:

    foreach (var filePath in Directory.EnumerateFiles(@"c:\folder"))
    {
        using (var reader = new StreamReader(filePath))
        {
            var line = reader.ReadLine();
            listBox.Items.Add(line);
        }
    }

Upvotes: 1

Related Questions