从指定文件夹中存在的所有文件读取文件内容

本文关键字:文件 读取 文件夹 存在 | 更新日期: 2023-09-27 18:12:04

我必须从特定文件夹中存在的所有文件中读取文件内容。你们能帮我解决这个问题吗?

 DirectoryInfo d = new DirectoryInfo(@"D:'Test");
 FileInfo[] Files = d.GetFiles("*"); 
 string str = "";
 foreach(FileInfo file in Files )
{
  **//Do something here to get the file contents..!**
} 

PS:ReadAllText不工作!和StreamReader给出错误:

非静态字段、方法或属性需要对象引用。

从指定文件夹中存在的所有文件读取文件内容

using System.IO;
...
foreach (string file in Directory.EnumerateFiles(folderPath, "*.*"))
{
    string contents = File.ReadAllText(file);
}

以上是。net 4.0的特性;在以前的版本中,您需要将EnumerateFiles替换为GetFiles。