1

i am trying to get a list of files into an array or list from multiple directories

currently i am doing:

tempbatchaddresses = Directory.GetFiles(@"c:\", "*.log");

but i also need tempbatchaddresses += Directory.GetFiles(@"d:\", "*.log");

and a third one as well. i need to add the file locations of files from 3 different directories.

how do i do this?

3 Answers 3

7
tempBatchAddresses = Directory.GetFiles(@"c:\", "*.log").ToList();

tempBatchAddresses.AddRange(Directory.GetFiles(@"d:\", "*.log").ToList());

tempBatchAddresses.AddRange(Directory.GetFiles("some dir", "some pattern").ToList());

and so on ..

Sign up to request clarification or add additional context in comments.

Comments

4

Try something like this:

List<string> myFiles = new List<string>();
myFiles.AddRange(Directory.GetFiles(@"c:\", "*.log"));
...etc...

foreach (string file in myFiles)
{
    //do whatever you want
}

Comments

1

There are a myriad number of similar ways to tackle the problem. Here's one.

static void Main()
{
    IEnumerable<string> files = GetFiles("*.log", @"C:\", @"D:\", @"E:\");
}

static IEnumerable<string> GetFiles(string searchPattern, params string[] directories)
{
    foreach (string directory in directories)
    {
        foreach (string file in Directory.GetFiles(directory, searchPattern))
            yield return file;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.