Sara
Sara

Reputation: 93

How to read text file inside folder in c#

I have 5 different text file inside the Main Folder example

c:\Main

inside Main, i have 5 file (test1.txt, test2.txt. test3.txt, test4.txt and test5.txt)

How to read the latest file?

Upvotes: 2

Views: 738

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222522

using System.Linq;

//Get LatestFile  
var directory = new DirectoryInfo("c:\\Main");
var LatestFile = (from f in directory.GetFiles()
             orderby f.LastWriteTime descending
             select f).First();
//Read contents 
 string contents = File.ReadAllText(LatestFile);

Upvotes: 10

Related Questions