Hi anjali600,
Refer the below sample code.
Namespaces
C#
using System.IO;
using System.Linq;
VB.Net
Imports System.IO
Imports System.Linq
Web Application
Code
C#
DirectoryInfo di = new DirectoryInfo(Server.MapPath("~/Files"));
var extensions = new[] { "*.jpg", "*.png" };
FileInfo file = extensions
.SelectMany(ext => di.GetFiles(ext))
.OrderByDescending(x => x.LastWriteTime)
.FirstOrDefault();
VB.Net
Dim di As DirectoryInfo = New DirectoryInfo(Server.MapPath("~/Files"))
Dim extensions = {"*.jpg", "*.png"}
Dim file As FileInfo = extensions _
.SelectMany(Function(ext) di.GetFiles(ext)) _
.OrderByDescending(Function(x) x.LastWriteTime) _
.FirstOrDefault()
Screenshots
Files Location
File Info
Windows Application
Code
C#
private void Form1_Load(object sender, EventArgs e)
{
DirectoryInfo di = new DirectoryInfo("E:\\Files");
var extensions = new[] { "*.jpg", "*.png" };
FileInfo file = extensions
.SelectMany(ext => di.GetFiles(ext))
.OrderByDescending(x => x.LastWriteTime)
.FirstOrDefault();
}
VB.Net
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim di As DirectoryInfo = New DirectoryInfo("E:\\Files")
Dim extensions = {"*.jpg", "*.png"}
Dim file As FileInfo = extensions _
.SelectMany(Function(ext) di.GetFiles(ext)) _
.OrderByDescending(Function(x) x.LastWriteTime) _
.FirstOrDefault()
End Sub