I am looking to iterate through two directories. I am aware of options available to iterate through each directory and storing the list of all the details it found within it in a list. 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.The requirement is while iterating through one of the directory for all its contents, I want to compare each item in that directory to the one in the second directory
Asked
Active
Viewed 531 times
-7
-
1Why do you need this? Do you just want arbitrary pairs of files? Or do you want to find something like matching files? It could be as simple as `Directory.EnumerateFiles(...).Zip(Directory.Enumerate.Files(...), ...)...`... – Haukinger Oct 18 '16 at 15:29
-
1What is your exact requirement? It may be solved with linq joins. – Mohammad Arshad Alam Oct 18 '16 at 15:29
-
@CodeCaster i haven't tried that as i didn't even know whether there was any other of achieving what I need other than what I have mentioned. – Ajit Oct 18 '16 at 15:30
-
@Haukinger this is still not simultaneously, he should consider a multithreaded approach in order to acheive this – meJustAndrew Oct 18 '16 at 15:31
-
4This question, as asked, both feels too broad and smells of being an [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem/66378#66378). Instead of asking how to achieve some unknown problem (A), you are asking about a solution you think might work (simultaneous iteration (B)). Without knowing what A is, it's not really possible to tell you how to implement B. – Claies Oct 18 '16 at 15:32
-
@Arshad I am trying to iterate through the contents of two directory paths as the same time – Ajit Oct 18 '16 at 15:33
-
2"the same time" is a loaded phrase. The I/O system doesn't allow for simultaneous reads, so it's impossible in any language. Clearly state what you're *actually trying to do*. – itsme86 Oct 18 '16 at 15:37
-
1@Pete Please mention your objective. What you want to do by iterating 2 directories simultaneuosly? – Amey Kamat Oct 18 '16 at 15:37
-
@Claies it isn't some thing unknown. I just was curious whether it is something that can be achieved in C# because I know that it can be done in other languages. – Ajit Oct 18 '16 at 15:37
-
1right, the **potential solution** is known, since it is what you are asking about, but the **problem** you plan to use this solution for is what is unknown. multiple people have asked you to explain **why** you need to iterate two directories at the same time, to which you just keep saying "because I want to" or "you can do it in other languages", which doesn't explain anything at all. – Claies Oct 18 '16 at 15:41
-
@Claies the answer to your "why" is while iterating through one of the directory for all its contents, I want to compare each item in that directory to the one in the second directory. – Ajit Oct 18 '16 at 15:45
-
ok, so that's what you should put in the question body. That seems like something reasonable to accomplish, and there are probably quite a few patterns in place already that can achieve this. – Claies Oct 18 '16 at 15:47
-
Question is not very clear. Iterating through a directory is not normally a performance-impacting task. One would normally try multithreading and asynchronous programming – usr-local-ΕΨΗΕΛΩΝ Oct 18 '16 at 15:48
-
@Claies could you please mention some of these patterns. – Ajit Oct 18 '16 at 15:51
1 Answers
6
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
}

Eric Lippert
- 647,829
- 179
- 1,238
- 2,067
-
2
-
3@Pete: Remember, "simultaneous" has a very specific meaning; it implies that two things are actually happening physically at the same time, and that is very hard to do with a single disk. Timings of I/O events can be very tricky; and if you have a performance problem involving high latency waiting on disks, then you don't want simultaneity, you want the opposite. You want an asynchronous workflow. – Eric Lippert Oct 18 '16 at 18:01