Ans: Here is one solution for getting latest file name from folder using c# code
Call Function as follows:
FileInfo newestFile = GetNewestFile(new DirectoryInfo(@"D:\DatabaseFiles"));
Function:
public static FileInfo GetNewestFile(DirectoryInfo directory)
{
return directory.GetFiles()
.Union(directory.GetDirectories().Select(d => GetNewestFile(d)))
.OrderByDescending(f => (f == null ? DateTime.MinValue : f.LastWriteTime))
.FirstOrDefault();
}
Now my question is someone please tell me alternative way to get newest file name from folder?
Doing an entire sort just to find the largest item in a list is extremely inefficient.
You would be better using one of the "MaxBy()" Linq extensions to find the maximum value, such as the MoreLinq one from Jon Skeet and others. (The full library is here.)
If you use MaxBy()
the code might look something like this:
public static FileInfo GetNewestFile(DirectoryInfo directory)
{
return directory.GetFiles()
.Union(directory.GetDirectories().Select(d => GetNewestFile(d)))
.MaxBy(f => (f == null ? DateTime.MinValue : f.LastWriteTime));
}
Ideally you would combine this with the other suggested answer (i.e. use the overload of Directory.EnumerateFiles()
which does the recursion for you).
Here's a complete console app sample. The "MaxBy()" method was sourced and somewhat modified from an old version of MoreLinq:
using System;
using System.Collections.Generic;
using System.IO;
namespace Demo
{
public static class Program
{
private static void Main()
{
string root = "D:\\Test"; // Put your test root here.
var di = new DirectoryInfo(root);
var newest = GetNewestFile(di);
Console.WriteLine("Newest file = {0}, last written on {1}", newest.FullName, newest.LastWriteTime);
}
public static FileInfo GetNewestFile(DirectoryInfo directory)
{
return directory.EnumerateFiles("*.*", SearchOption.AllDirectories)
.MaxBy(f => (f == null ? DateTime.MinValue : f.LastWriteTime));
}
}
public static class EnumerableMaxMinExt
{
public static TSource MaxBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> selector)
{
return source.MaxBy(selector, Comparer<TKey>.Default);
}
public static TSource MaxBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> selector, IComparer<TKey> comparer)
{
using (IEnumerator<TSource> sourceIterator = source.GetEnumerator())
{
if (!sourceIterator.MoveNext())
{
throw new InvalidOperationException("Sequence was empty");
}
TSource max = sourceIterator.Current;
TKey maxKey = selector(max);
while (sourceIterator.MoveNext())
{
TSource candidate = sourceIterator.Current;
TKey candidateProjected = selector(candidate);
if (comparer.Compare(candidateProjected, maxKey) > 0)
{
max = candidate;
maxKey = candidateProjected;
}
}
return max;
}
}
}
}