-1

Can we get files from different location using some Built-In Function in C# without any loop. Like if I have following paths

C:\Folder1
C:\abc\Folder2
D:\Folder3

I want to get all files from Folder1, Folder2 and Folder3 at same time without using any loop.

tiago
  • 22,602
  • 12
  • 72
  • 88
Dodhytech
  • 1
  • 3
  • 1
    There is no API available for that. You need to aggregate files from different folders by either using LINQ, loops or something else. – oleksii May 26 '14 at 10:09
  • What are you really trying to accomplish? Even if you got all the files, the odds are you'd need to use a loop to actually use the file paths. – Sayse May 26 '14 at 10:15
  • Why don't you want to use any loop? – brothers28 May 26 '14 at 10:27
  • I want to save these file locations in Microsoft SQL database by using BULK INSERT. – Dodhytech May 28 '14 at 10:01

2 Answers2

1

According to MSDN, you can search for files in a single directory.

For example:

Directory.GetFiles("C:\Folder1")

You just need to adapt, however an extension method is not possible since it's a static class.

More info here: http://msdn.microsoft.com/en-us/library/07wt70x2(v=vs.110).aspx

Basiccly, it means that a loop is required to do search for all the paths. Otherwise, it's not possible.

Complexity
  • 5,682
  • 6
  • 41
  • 84
0

A loop is required there is no built-in function for that.

You can maintain List<string> for the purpose.

Example:

List<string> lstPaths = new List<string>();
lstPaths.Add(@"C:\Folder1");
lstPaths.Add(@"C:\abc\Folder2");
lstPaths.Add(@"D:\Folder3");

foreach(string sPath in lstPaths)
{
    string[] arrFiles = Directory.GetFiles(sPath);

    //you can loop through arrFiles here
}
Hassan
  • 5,360
  • 2
  • 22
  • 35