Monday, February 6, 2012

Loop through all the files in C# 2010

Hi All,

I migrated from VBA to C# a while back. Earlier, with VBA, I used to create Re-Cursive function to loop through all inner folders in a given folder. For example, if I have to search whole files in C: Drive, I will probably need to loop through Drive, then folders and subfolders in VBA. But in C#, you can simply give a parameter to search through the subdirectories as well.


DirectoryInfo parent = new DirectoryInfo(@"C:\Vikas");
FileInfo[] files = parent.GetFiles("*.*", SearchOption.AllDirectories);

The files array will contain all the files within the Folder "Vikas" and it's subfolder.

Vikas B

No comments: