How to loop through all files in a folder using C#
Someone asked me how to loop through all the files in a folder...
well just a quick sample recursive solution...
Update: Thanks to Avner's comment I've noticed that the original code I've posted here was vulnerable to File System Reparse Point issue.
What is File System Reparse Point issue ?
When compiling the code and running if you add a reparse point to (c:\temp\recursive1\recursive2 -> c:\temp\recursive1). After a few recursions you get “System.IO.IOException: The name of the file cannot be resolved by the system.”. At the time of failure it was looking for c:\temp\recursive1\recursive2\recursive2\recursive2\recursive2\recursive2\recursive2\recursive2\recursive2\rerecursive2\recursive2\recursive2
MS Press Writing Secure Code 2 pages 686-7 includes the following information regarding File System Reparse Point Issues:
"Starting in windows 2000 NFTS supports directory junctions. This is similar to a UNIX symbolic link that redirects a reference from one directory to another directory on the same machine. You can create and manage directory junctions using Linkd.exe a tool available in the Windows Resource Kit."
"Directory junctions present a threat to any application that does a recursive traversal of the directory structure."
"It is the responsibility of any application that scans the directory hierarchy and especially the responsibility of applications that make destructive changes recursively through the directory hierarchy to recognize directory junctions and avoid traversing through them, Because directory junctions are implementer using reparse points, applications should see if a directory has the FILE_REPARSE_POINT attribute set before processing that directory. Your code is safe if you do not process any directory with FILE_REPARSE_POINT set which you can verify with functions such as GetFileAttributes and lpFindFileData->dwFileAttributes in FindFirstFile."
While looking for a gui tool to create Symbolic files I've found Winbolic Link which can create NTFS Junction and Shell Links
The program creates these links using only built-in functionality of Windows. It does not extend or modify the functionality of Windows or the file system, or require any service to remain running to maintain functionality. It merely creates the special kinds of files and folders Windows already supports, but which Microsoft did not distribute tools to create.
You can see that now I've implemented the check for ReparsePoint and my code is safe again :-)
Ohad.
// How much deep to scan. (of course you can also pass it to the method)
const int HowDeepToScan=4;
public static void ProcessDir(string sourceDir, int recursionLvl)
{
if (recursionLvl<=HowDeepToScan)
{
// Process the list of files found in the directory.
string [] fileEntries = Directory.GetFiles(sourceDir);
foreach(string fileName in fileEntries)
{
// do something with fileName
Console.WriteLine(fileName);
}
// Recurse into subdirectories of this directory.
string [] subdirEntries = Directory.GetDirectories(sourceDir);
foreach(string subdir in subdirEntries)
// Do not iterate through reparse points
if ((File.GetAttributes(subdir) &
FileAttributes.ReparsePoint) !=
FileAttributes.ReparsePoint)
ProcessDir(subdir,recursionLvl+1);
}
}