Visual Studio 2010!

Read now >

View Now
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.
ADVERTISEMENT
ADVERTISEMENT

 

DevSource.com: Your Source for Visual Studio on Facebook
ADVERTISEMENT
Recursive Directory Searches in C# (Part 1)
By Rick Leinecker

Rate This Article: Add This Article To:

Recursive Directory Searches in C# (Part 1) - ' Adding Recursion '
( Page 3 of 3 )

Calling a Recursive Directory Method

As we found in the previous section, there is an indefinite number of nested subdirectories in a device's directory tree. For this reason, it would be impractical to nest a bunch of foreach loops to insure that we go deep enough. It's also a poor programming practice since it places an arbitrary limit on the depth of a search.

The solution is to use a recursive method that can be called as many times as necessary. For each new subdirectory level, the method can be called again. In this way there is no limit to the number of embedded subdirectories that can be examined.

The following simple method allows us to traverse the entire directory. Once it finds a subdirectory, it calls itself recursively passing the new subdirectory name.

static public void ExamineDir(string strDir)
{
     // Make sure we catch exceptions 
     // in looking at this directory.
     try
     {
          // Loop through the list of directories.
          foreach (string strDirName
               in Directory.GetDirectories(strDir))
          {
               // Display the directory name.
               Console.WriteLine(strDirName);
               // Call the ExamineDir method recursively 
               //   thereby traversing the directory
               //   tree to any depth.
               ExamineDir(strDirName);
          }
     }
     catch{}
}

The ExamineDir method can be called to examine the device starting at the root as follows:

ExamineDir(@"C:\");

The ExamineDir method can also be called to examine a directory that does not start at the root level (and its subdirectories ) as follows:

ExamineDir(@"C:\Windows\System32");

The ExamineDir method can be enhanced so that you can view the level of recursion by adding display indentions. For instance, root level directories will not be indented when displayed by the Console.WriteLine method. First level subdirectories are indented when displayed by the Console.WriteLine method. Second level subdirectories are indented even further. The following enhancement to the ExamineDir method shows you a simple addition that allows the display to reflect the level of depth of the subdirectories:

static public void ExamineDir(string strDir,int nIndex)
{
     try
     {
          foreach (string strDirName 
               in Directory.GetDirectories(strDir))
          {
               string strIndent = new String
                    ( ' ', nIndex );
               Console.WriteLine(
                    strIndent + strDirName);
               ExamineDir(strDirName, nIndex+1);
          }
     }
     catch{}
}

Make sure, though, when you can the enhanced ExamineDir method that you specify an indention value of 0 as follows:

ExamineDir(@"C:\", 0);

Next Time

Next week, we'll continue this discussion and talk about how to add threading to the method. This will help prevent the CPU from flying all the way to the top and will allow for a program that operates better on the user's computer.



 
 
>>> More Microsoft Languages Articles          >>> More By Rick Leinecker