如何阅读和显示多个文本文件中的内容?

时间:2019-02-26 12:40:46

标签: c# visual-studio streamreader

作为一个问题,如何显示多个文本文件中的内容?我想按一下按钮从控制台中显示的路径中读取所有内容,并将其存储到变量中。目前,它只能一一读取。我目前是C#的自学和初学者。

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.");
                }
            }

        }
    }
}

2 个答案:

答案 0 :(得分:0)

您可以尝试这样

   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.");
            }
        }

    }

答案 1 :(得分:0)

static void Main(string[] args)
    {
        //ListFileInDirectory(@"C:\Users\albto\Documents\_projetos\MultiplesFiles\MultiplesFiles\_arquivos");
        ListFileInDirectory(@"C:\Users\liewm\Desktop\L907C524_1x");

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


    private static void ListFileInDirectory(string workingDirectory)
    {
        string[] filePaths = Directory.GetFiles(workingDirectory);
        List<string> allLines = new List<string>();

        foreach (string filePath in filePaths)
        {
            try
            {
                //stores the files address
                allLines.Add(filePath);

                //stores file lines
                allLines.AddRange(System.IO.File.ReadAllLines(filePath));
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
            }
            finally
            {
                Console.WriteLine("Executing finally block.");
            }
        }

        //shows all stored lines
        allLines.ForEach(Console.WriteLine);
        Console.ReadLine();

    }