如何读取和显示来自多个文本文件的内容?
How to read and show content from multiple text file?
如题,如何显示多个文本文件的内容?我想用 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.");
}
}
}
}
}
你可以这样试试
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.");
}
}
}
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();
}
如题,如何显示多个文本文件的内容?我想用 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.");
}
}
}
}
}
你可以这样试试
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.");
}
}
}
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();
}