I was wondering if there is a better way of achieving this in C# without iterating through each directory and storing the details in a separate list.
Use EnumerateFiles
on both directories, zip-join them, and then run the join through a foreach loop.
var firstFiles = Directory.EnumerateFiles(...);
var secondFiles = Directory.EnumerateFiles(...);
var joined = firstFiles.Zip(secondFiles,
(first, second) => new { First = first, Second = second });
foreach(var pair in joined)
{
// now do something with pair.First and pair.Second
}
2
solved Iterate through two directories [closed]