Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Getting newest file from specific directory

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 390
    Comment on it

    If you want to get newest file from any specific directory, you can use the following source code:

    C# code

    FileInfo GetNewestFile(DirectoryInfo directoryInfo)
    {
      return directoryInfo.GetFiles()
          .Union(directoryInfo.GetDirectories().Select(d => GetNewestFile(d)))
          .OrderByDescending(f => (f == null ? DateTime.MinValue : f.LastWriteTime))
          .FirstOrDefault();
    }

    VB.Net code

    Function GetNewestFile(ByVal directoryInfo As DirectoryInfo) As FileInfo
     Return directoryInfo.GetFiles()
      .Union(directoryInfo.GetDirectories().Select(d => GetNewestFile(d)))
      .OrderByDescending(f => (f = IIf(Nothing, DateTime.MinValue, f.LastWriteTime)))
      .FirstOrDefault()
    End Function

    Happy Coding!!

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: