Search code examples
c#visual-studiostreamreader

How to read and show content from multiple text file?


As the question, how to show content from multiple text file? I would like to read all the content from the path show in the console with 1 press button and store into a variable. Currently it only can read 1 by one. I'm self learner and begineer for the C# currently.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace ConsoleApp1
{

    class Program
    {
        static void Main(string[] args)
        {
            ListFileInDirectory(@"C:\Users\liewm\Desktop\SampleFile");

            Console.WriteLine("Press enter to continue");
            Console.Read();
        }

        private static void ListFileInDirectory(string workingDirectory)
        {
            string[] filePaths = Directory.GetFiles(workingDirectory);
            String line;

            foreach (string filePath in filePaths)
            {
                Console.WriteLine(filePath);
                try
                {
                    //Pass the file path and file name to the StreamReader constructor
                    StreamReader sr = new StreamReader(filePath);

                    //Read the first line of text
                    line = sr.ReadLine();

                    //Continue to read until you reach end of file
                    while (line != null)
                    {
                        //write the lie to console window
                        Console.WriteLine(line);
                        //Read the next line
                        line= sr.ReadLine();
                    }

                    //close the file
                    sr.Close();
                    Console.ReadLine();
                }
                catch (Exception e)
                {
                    Console.WriteLine("Exception: " + e.Message);
                }
                finally
                {
                    Console.WriteLine("Executing finally block.");
                }
            }

        }
    }
}

Solution

  • You can try like this

       private static void ListFileInDirectory(string workingDirectory)
        {
            string[] filePaths = Directory.GetFiles(workingDirectory);
            String line;
    
            foreach (string filePath in filePaths)
            {
                Console.WriteLine(filePath);
                try
                {
    
                  //it returns string[]
                 var allText = System.IO.File.ReadAllLines(filePath);//opens a text file, reads all text and closes the text file.
    
                 foreach(var str in allText)
                 {
                   //Do what you want.
                 }
    
                }
                catch (Exception e)
                {
                    Console.WriteLine("Exception: " + e.Message);
                }
                finally
                {
                    Console.WriteLine("Executing finally block.");
                }
            }
    
        }