Hi,
i have an algorithm that searches into a directory and search for all the text files in that directory and any sub-directory. Assuming i do not know how many sub-directories and sub-sub directories there are in the parent directory. how do i calculate the complexity?
this is the code i am using
public List<string> GetFilesInDirectory(string directoryPath)
{
// Store results in the file results list.
List<string> files = new List<string>();
// Store a stack of our directories.
Stack<string> stack = new Stack<string>();
// Add initial directory.
stack.Push(Server.MapPath(directoryPath));
// Continue while there are directories to process
while (stack.Count > 0)
{
// Get top directory
string dir = stack.Pop();
try
{
// Add all files at this directory to the result List.
files.AddRange(Directory.GetFiles(dir, "*.txt"));
// Add all directories at this directory.
foreach (string dn in Directory.GetDirectories(dir))
{
stack.Push(dn);
}
}
catch(Exception ex)
{
}
}
return files;
}
thanks