Um...That's some odd code you have there.
Each time you go round the loop, you collect some information, and immediately throw it away - none of your data is available outside the loop as it goes out of scope when the loop ends, or available to the next iteration as you overwrite it with new variables - so the whole loop is currently redundant and can be removed.
I'm not exactly sure what you are trying to do, but to start with, move the first union outside the loop as nothing will change it:
string[] drives = Directory.GetLogicalDrives();
string[] listPaths = fde.Union(falaa).ToArray();
foreach (string d in drives)
{
string[] d1 = Directory.GetDirectories(d);
string[] pathlist = listPaths.Union(d1).ToArray();
}
Then, I'd create an external list to hold the data while I was assembling it:
string[] drives = Directory.GetLogicalDrives();
string[] listPaths = fde.Union(falaa).ToArray();
List<string> paths = new List<string>();
foreach (string d in drives)
{
string[] d1 = Directory.GetDirectories(d);
paths.AddRange(listPaths.Union(d1));
}
The
paths
collection is then available outside the loop.