My task is to print out every subdirectory without using a recursion. I have to use two functions, one will fill the array with every subdirectory and then print this array, the other is for resizing the array. I know this is a bad practice that I have to resize array every time I want to add something to it, but it is how I should do it. Also, I'm not allowed to use the function of getting the parent of a directory and SearchOption.AllDirectories. It has to be done using loops.
class Program {
static void Main(string[] args) {
string path = @"D:\Heaven Benchmark 4.0";
WriteDirectories(path);
Console.ReadKey();
}
static void WriteDirectories(string path) {
string[] dirs = Directory.GetDirectories(path);
string[] allDirs = new string[0];
for (int i = 0; i < allDirs.Length; i++) {
Console.WriteLine(allDirs[i]);
}
}
static void ResizeArray(ref string[] arr, int newSize) {
string[] newArr = new string[newSize];
for (int i = 0; i < newSize - 1; i++) {
newArr[i] = arr[i];
}
arr = newArr;
}
}
I'm thinking about filling array allDirs with every existing subdirectory of a path and then printing it out. is there any better and easier way of doing this?