title: C# - Get LastWriteTime of A Folder's Descendants
body: <p>How can I retrieve the latest write time of <strong>any</strong> descendant of a folder?</p>
  
  <p>I need a method that will return the paths of all files that were modified after a specified DateTime. I figured I could save a lot of expensive disk reading by making sure the directory <a href="http://msdn.microsoft.com/en-us/library/system.io.filesysteminfo.lastwritetime.aspx" rel="nofollow">LastWriteTime</a> is within the specified range before going through the trouble of iterating through its files and subdirectories.</p>
  
  <p>This works in that when a file within the top level of a directory changes, the last write time of the folder is updated as well. However, the last write time does not bubble up any further than the file's immediate parent. In other words, if the grandchild file is changed, its property is updated, and so is it's father folder's, but not the grandfather.</p>
  
  <p>Is there another high-level indicator I can use to accomplish this, or will I have to resort to recursing through every folder regardless of last change time?</p>
  
  <p>Here is the method as it currently stands:</p>
  
  <pre><code>private void AddAllFilesOfAllSubdirsWithFilterToList(string dirPath, ref List&lt;FileInfo&gt; filesList, DateTime minDate)
  {
      // Return files in this directory level
      foreach (string filePath in Directory.GetFiles(dirPath, "*.*", SearchOption.TopDirectoryOnly))
      {
          FileInfo fileInfo = new FileInfo(filePath);
          if (fileInfo.LastWriteTimeUtc &gt; minDate)
          {
              filesList.Add(fileInfo);
          }
      }
  
      // Return recursive searches through sudirs
      foreach (string subDirPath in Directory.GetDirectories(dirPath))
      {
          DirectoryInfo dirInfo = new DirectoryInfo(subDirPath);
          if (dirInfo.LastWriteTimeUtc &gt; minDate)
          {
              GetAllFilesOfAllSubdirsWithFilter(subDirPath, ref filesList);
          }
      }
  }
  </code></pre>