Nøgleordet er rekursivitet. Her er noget fin kode som kan udskrive alle directories. Det skal naturligvis tilpasses dit program:
class Recursive {
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args) {
GetFiles("E:/");
GetSubFolders("E:/");
Console.Read();
}
static void GetFiles(string directory) {
try {
string [] files = Directory.GetFiles(directory);
Console.WriteLine("Files in folder \"{0}\"", directory);
foreach(string f in files) {
Console.WriteLine(f);
}
}
catch(System.UnauthorizedAccessException) {
Console.WriteLine("Access not allowed to \"{0}\"", directory);
}
}
static void GetSubFolders(string directory) {
try {
//Get files for this folder
GetFiles(directory);
string [] subFolders = Directory.GetDirectories(directory);
foreach(string sf in subFolders) {
GetSubFolders(sf);
}
}
catch(System.UnauthorizedAccessException) {
Console.WriteLine("Access not allowed to \"{0}\"", directory);
}
}
Hvis du ikke gider rode med det, er der én der har lavet det samme som det du vil. Se blot på:
http://www.codeproject.com/csharp/RecursiveFileExplorer.asp