private void TraverseDFS(DirectoryInfo dir) { try { // To get the subFolder of main directory DirectoryInfo[] children = dir.GetDirectories(); root = new MyFolder(dir.FullName); // initialize the array length of MyFolder field in class this.root.AddMyFolderSize(children.Length); for (int i = 0; i < children.Length; i++) { // get the name of folder in name field of MyFolder class this.root.childFolders[i].name = root.name + "\\" + children[i].Name; // get the files of sub-folder FileInfo[] files = children[i].GetFiles(); this.root.childFolders[i].AddFileSize(files.Length); for (int j = 0; j < files.Length; j++) { this.root.childFolders[i].files[j].name = files[j].ToString(); this.root.childFolders[i].files[j].size = files[j].Length; } //TraverseDFS(children[i]); // occurs problem but still i can solve this if instead of recursion we call other method who take the sub-folder of current folder and put in childfolder to current childfolder } } catch (Exception) { Console.WriteLine("Exception Occurs"); } }
public void AddMyFolderSize(int size) { childFolders = new MyFolder[size]; for (int i = 0; i < size; i++) { childFolders[i] = new MyFolder(); } }
public void PrintDFS(MyFolder root, string space) { Console.WriteLine(space + root.name); if (root.childFolders == null) { return; } for (int i = 0; i < root.childFolders.Length; i++) { if (root.childFolders[i].files == null) { return; } for (int j = 0; j < root.childFolders[i].files.Length; j++) { fileSize += (ulong)root.childFolders[i].files[j].size; Console.WriteLine(root.childFolders[i].files[j].name); } PrintDFS(root.GetFolder(i), space + " "); } }