title: C# How to loop through the source folder or subfolders that match the destination
body: <p>I need to overwrite files from a source to a destination directory.
  The structure of each folder is different so i'm trying to do it in a generic way. 
  The thing is, each folder (source and destination) could have numerous subdirectories or none at all. </p>
  
  <p>The code I currently have is this:</p>
  
  <pre><code>//copy and overwrite the files depending on whatever is in the destination
  //search through the destination to find the file
  foreach (var dstfile in Directory.GetFiles(targetDir))
  {
    //search through the source to find the matching file
    foreach (var srcfile  in Directory.GetFiles(sourceDir))
    {
       //cut off the source file from the source path 
       strSrcFile = srcfile.Split(Path.DirectorySeparatorChar).Last();
       strDstFile = dstfile.Split(Path.DirectorySeparatorChar).Last();
  
      //if the destination and source files match up, replace the desination with the source
      if (strSrcFile == strDstFile)
      {
        File.Copy(srcfile, Path.Combine(targetDir, Path.GetFileName(strSrcFile)), true);
      }
    }
  }
  
  
  //look through the subfolders to see if any files match up 
  foreach (var srcFolder in Directory.GetDirectories(sourceDir))
  {
     //search through the source for the files 
     foreach (var srcFile in Directory.GetFiles(srcFolder))
     {
        //search through the destination for the files 
        foreach (var dstFile in Directory.GetFiles(targetDir))
        {
  </code></pre>
  
  <p>As you can see there are a lot of foreach loops, is there a way to streamline this?</p>