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?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Apr 8, 2014, 2:15 PM
using System;
using System.IO;
using System.Linq;
class Program
{
static void Main()
{
var di = new DirectoryInfo(@"c:\Main");
string search = "test*.txt";
FileInfo latest = di.GetFiles(search).OrderBy(fi => fi.CreationTime).Last();
string text = File.ReadAllText(latest.FullName);
Console.WriteLine(text);
Console.ReadKey();
}
}
Lakshmanan Sethu SankaranarayanPosted Apr 8, 2014, 6:05 AM