Saturday, December 10, 2005
by Nik Kalyani
Saturday, December 10, 2005 6:42:12 AM (Pacific Standard Time, UTC-08:00)

If you do any file processing, you will inevitably find yourself writing recursive code to traverse a directory structure. If you have a directory structure that is broad and deep with files/folders numbering in the thousands, recursion is inefficient as it consumes stack resources. In some cases you will run out of stack space and cause the code to throw an error.

You can do this more efficiently by using a seldom-used class of the System.Collections namespace: Stack. The Stack class implements a LIFO data structure (Last In First Out), as opposed to Queue, which implements FIFO (First In First Out). Instead of deferring to the run-time to implement the stack (which is essentially what recursion does, but in an inefficient way), you can optimize the process and also prevent your code from breaking, by managing the stack yourself.

Here is a simple example that will traverse and print-out all the files in a directory structure without using recursion:

private void TraverseFolder(DirectoryInfo dir)
{
	Stack stack = new Stack();
	stack.Push(dir);
	while (stack.Count > 0)
		{
			DirectoryInfo dirInfo = (DirectoryInfo) stack.Pop();
			foreach(FileInfo file in dirInfo.GetFiles())
			{
				System.Web.HttpContext.Current.Response.Write(file.FullName + "<br>");
			}					   
			DirectoryInfo[] subDirs = dirInfo.GetDirectories();
			for (int index = subDirs.Length - 1; index >= 0; index -= 1)
				stack.Push(subDirs[index]);
		}
}

 

RSS feed
Search and Links
Bling

View Nik Kalyani's profile on LinkedIn

Contact me: nik*kalyani.com (replace "*")

TechBubble
www.flickr.com
This is a Flickr badge showing public photos from techbubble. Make your own badge here.
Statistics
Total Posts: 214
This Year: 32
This Month: 0
This Week: 0
Comments: 238
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008
Nik Kalyani
Sign In
All Content © 2008, Nik Kalyani