/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example15_8.cs illustrates recursive Directory use
*/
using System;
using System.IO;
public class Example15_8
{
// ShowDirectory prints the directory name
// and retrieves its children
public static void ShowDirectory(DirectoryInfo di, int intLevel)
{
try
{
// print out the directory name, after 2*intLevel spaces
string strPad = new String(' ', 2*intLevel);
Console.WriteLine(strPad + di.Name);
// get its children and recursively call this routine
// with one more level of indenting
foreach (DirectoryInfo diChild in di.GetDirectories())
ShowDirectory(diChild, intLevel+1);
}
catch {} // just keep going in case of any error
finally{}
}
public static void Main()
{
// create a DirectoryInfo object
DirectoryInfo di = new DirectoryInfo("c:\\");
// And pass it to the recursive printing routine
ShowDirectory(di, 0);
}
}
|