Description : Following code is used to prints the directory name and retrieves its children in treeview
Used Name Spaces
using System; using System.IO;
Code Segment
public class Example15_8 {
public static void ShowDirectory(DirectoryInfo di, int intLevel) { try { string MystrPad = new String(' ', 2*intLevel); Console.WriteLine(MystrPad + di.Name);
foreach (DirectoryInfo MydiChild in di.GetDirectories()) ShowDirectory(MydiChild, intLevel+1); } catch {} finally{} }
public static void Main() { DirectoryInfo MyDirectoryInfo = new DirectoryInfo("c:\\"); ShowDirectory(MyDirectoryInfo, 0);
}
}
Code Explanation
1. create a DirectoryInfo object 2. pass it to the recursive printing routine 3. print out the directory name, after 2*intLevel spaces 4. get its children and recursively call this routine with one more level of indenting
By Nathan
|
No responses found. Be the first to respond and make money from revenue sharing program.
|