title: Getting Random File from Directory Tree
body: <p>So I can't seem to find a good example of this anywhere.  I found a good example from <a href="https://stackoverflow.com/questions/742887/select-random-file-from-directory">this</a> question of picking a random file from a directory, but I need to pick a random file from a directory tree (of unknown depth).  This code gets a random file from 1 directory, but I have been perplexed as to how to extend it to all sub directories as well, I'm sure one of you LINQ gurus out there can help me craft something.</p>
  
  <pre><code>var extensions = new string[] { ".mp3" };
  var di = new DirectoryInfo(MusicPath);
  var rgFiles = di.GetFiles("*.*")
      .Where( f =&gt; extensions.Contains( f.Extension.ToLower() ));
  int fileCount = rgFiles.Count();
  if (fileCount &gt; 0)
  {
      int x = this.Generator.Next( 0, fileCount );  //Generator is 'Random' object
      file = rgFiles.ElementAt(x).FullName;
  }
  </code></pre>