Going to parent folder in file explorer written in c# console

224 views Asked by At

UPDATED:

This code will work like a common WindowsFormApplication file explorer.

When you enter the id for each folder (each files and folders will have an id), it will show files and folders in it.

The thing i want is when user enter the ".." console go one step back (parent directory). I mean do somthing like "cd .." in CMD

This code will do the exploring in child folders (and its OK) but i don't know how to get back to the parent folder ( i know i should use Directory.GetParent and i know how to use it, i just want to know where should i put it in the code):

        while (true)
        {
            var select2 = Convert.ToInt32(Console.ReadLine());
                var dirDir = Directory.GetDirectories(dirDrive[select2]);
                var fileDir = Directory.GetFiles(dirDrive[select2]);
                for (var j = 0; j < dirDir.Length; j++)
                    Console.WriteLine("{0})\t{1}", j, Path.GetFileName(dirDir[j].TrimEnd(Path.DirectorySeparatorChar)));
                for (var k = dirDir.Length; k < fileDir.Length; k++)
                    Console.WriteLine("{0})\t{1}", k, Path.GetFileName(fileDir[k]));

                dirDrive = dirDir;
        }

Here you can see the codes that i added (commented codes) but it will crash absolutely because i don't have a folder with id ".." in my array, and it will say "Index was outside the bounds of the array."

 while (true)
        {
            var select2 = Convert.ToInt32(Console.ReadLine());
            //if (Convert.ToString(select2) != "..")
            //{
                var dirDir = Directory.GetDirectories(dirDrive[select2]);
                var fileDir = Directory.GetFiles(dirDrive[select2]);
                for (var j = 0; j < dirDir.Length; j++)
                    Console.WriteLine("{0})\t{1}", j, Path.GetFileName(dirDir[j].TrimEnd(Path.DirectorySeparatorChar)));
                for (var k = dirDir.Length; k < fileDir.Length; k++)
                    Console.WriteLine("{0})\t{1}", k, Path.GetFileName(fileDir[k]));

                dirDrive = dirDir;
            //}
            //else
            //{
            //    var dirDir = Directory.GetDirectories(Path.GetDirectoryName(dirDrive[select2]));//Error occurs here
            //    var fileDir = Directory.GetFiles(dirDrive[select2]);
            //    for (var j = 0; j < dirDir.Length; j++)
            //        Console.WriteLine("{0})\t{1}", j, Path.GetFileName(dirDir[j].TrimEnd(Path.DirectorySeparatorChar)));
            //    for (var k = dirDir.Length; k < fileDir.Length; k++)
            //        Console.WriteLine("{0})\t{1}", k, Path.GetFileName(fileDir[k]));
            //    dirDrive = dirDir;
            //}
        }

This is the whole code: pastebin , in case you want to try it.

MY QUESTION: I use this code for exploring through child folders. I want to know how can i get to parent folder ?

Thanks to all programmers helped me last time.

0

There are 0 answers