简体   繁体   中英

Read the file contents from all the files present in a specified folder

I have to read the file contents from all the files present in a specific folder. Can you guys help me out regarding this?

 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 is not working!! and StreamReader is giving error:

An object reference is required for non static field, method or property.

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

The above is a .NET 4.0 feature; in previous versions, you'll need to replace EnumerateFiles with GetFiles.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM