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

问题描述 投票:0回答:2

作为问题,如何显示来自多个文本文件的内容?我想通过1个按钮读取控制台中路径显示的所有内容并存储到变量中。目前它只能逐一阅读。我现在是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.");
                }
            }

        }
    }
}
c# visual-studio streamreader
2个回答
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.");
            }
        }

    }

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();

    }
© www.soinside.com 2019 - 2024. All rights reserved.