I have a root node A which contains B which contains C which contains D which contains an XML file abc.xml So in D:\ drive ,I have the following structure of directories A>>B>>C>>D. This path is dynamic. What is the best practice to read the file abc.xml in C# by iterating through the physical folders?
-
1If you do not know the location of the file you have no other choice but iterating all directories until you find what you are looking for. Was this the question or do you need code? If so, what have you tried so far? – Samuel Oct 09 '13 at 06:41
-
Yes exactly ..I have to iterate all the folders – user1907849 Oct 09 '13 at 06:47
-
No you don't, just use the correct overload of GetFiles with the SearchOption parameter – Panagiotis Kanavos Oct 09 '13 at 06:50
4 Answers
You could implement a recursive search algorithm that goes through all the folders and descends into the sub folders.
Pseudo Code:
public void GetXMLFilesRecursive(string currentFolder, List<string> results)
{
// Enumerate all directories of currentFolder
string[] folders = Directory.GetDirectories(currentFolder);
foreach (string folder in folders)
GetXMLFilesRecursive(folder, results));
// Enumerate all XML files in this folder only if it has no other sub-folders (is a leaf)
if (folders.Length == 0)
{
string[] xmlFiles = Directory.GetFiles(currentFolder, "*.xml");
results.AddRange(xmlFiles);
}
}
This method only returns XML files in the lowest folders of the hierarchy (i.e. folders that don't have sub folders). If you want all files you find along the way, comment out if (folders.Length == 0)
. On the other hand, you could then also use Directory.GetFiles
with SearchOption.AllDirectories
.
Why I wrote a recursive algorithm: The OP asked how to find all XML files in the leaf directories. You can not do that using Directory.GetFiles
with SearchOption.AllDirectories
, but you then need to implement the above.

- 55,956
- 8
- 91
- 139
-
I know to implement in a table structure in a database. But I dont know how to read from physical folder..Any APIs? – user1907849 Oct 09 '13 at 06:42
-
No reason to do this, GetFiles can search recursively already – Panagiotis Kanavos Oct 09 '13 at 06:50
-
@PanagiotisKanavos I know (and noted this in my answer), but if you want only files in the lowest folder in the hierarchy, that won't help you and you have to implement this recursive search. – Thorsten Dittmar Oct 09 '13 at 06:57
-
Going to edit my answer to make clearer why I wrote a recursive algorithm. – Thorsten Dittmar Oct 09 '13 at 06:58
-
The question wasn't how to get only leaf files. Even then, you don't need recursion, just do a GetDirectoryInfos(SearchOption.AllDirectories), pick the leafs out of this list and then do GetFileInfos on the leaf directories. – Panagiotis Kanavos Oct 09 '13 at 07:00
-
BTW you don't need to use a List<> to do a recursive call as long as you use an enumerator, eg. like the answer to: [Directory.EnumerateFiles => UnauthorizedAccessException](http://stackoverflow.com/questions/5098011/directory-enumeratefiles-unauthorizedaccessexception) – Panagiotis Kanavos Oct 09 '13 at 07:06
-
-
Since this answer got more attention than mine, you should also put the code in a try catch to avoid `UnauthorizedAccessException` and the `string[] folders` + `string[] xmlFiles` is also not needed each call, Then your answer will be like mine... – string.Empty Oct 09 '13 at 08:18
You can use Directory.GetFiles(d, "*.xml",SearchOption.AllDirectories)
to get all the xml files get what you are looking for.

- 6,400
- 5
- 29
- 42
-
The is 1 issue with this. If you do not have read permissions for 1 of the directory instances the entire loop fails. – string.Empty Oct 09 '13 at 06:58
You can search an entire tree for a file using Directory.GetFiles(path,searchPattern,SearchOption) or Directory.EnumerateFiles with SearchOption.AllDirectories, eg
var fullPaths=Directory.GetFiles(myPath,"abc.xml",SearchOption.AllDirectories)
You can also use the DirectoryInfo class to get full FileInfo instances instead of just the paths, with access to file properties and attributes:
var myDir=new DirectoryInfo(myPath);
var fileInfos=myDir.GetFiles("abc.xml",SearchOption.AllDirectories);
The difference between the GetFiles and EnumerateFiles methods is that the first returns an array with all the files found, blocking until it finds all of them. EnumerateFiles on the other hand returns results as it finds them, so you get to process the results much sooner.
What goes for GetFiles goes for the GetDirectories/EnumerateDirectories set of functions as well. The methods are available both from the Directory and DirectoryInfo class.
If you want to search for both directories and files, you can use GetFileSystemEntries/EnumerateFileSystemEntries to return both of them with a single call. The equivalent DirectoryInfo methods are GetFileSystemInfos/EnumerateFileSystemInfos

- 120,703
- 13
- 188
- 236
public List<string> getFiles(string path, string searchPattern, List<string> list)
{
try
{
foreach (string folder in Directory.GetDirectories(path))
getFiles(folder, searchPattern, list);
list.AddRange(Directory.GetFiles(path, searchPattern));
}
catch (UnauthorizedAccessException)
{
//Do not have access to the file.
}
return list;
}
Call like this:
//Get all xml files in the D drive:
List<string> files = getFiles(@"d:\", "*.xml", new List<string>());

- 10,393
- 4
- 39
- 67