Answer the question
In order to leave comments, you need to log in
How to order the output in the console?
You need to display all files and folders.
class Program
{
public static void Show(string dirPath)
{
DirectoryInfo dir = new DirectoryInfo(dirPath);
//Папки
string[] dirs = Directory.GetDirectories(dirPath);
//Файлы
FileInfo[] dirFiles = dir.GetFiles();
for (int i = 0; i < dirFiles.Length; i++)
{
Console.WriteLine(" " + dirFiles[i]);
}
for (int i = 0; i < dirs.Length; i++)
{
Console.WriteLine(dirs[i]);
Show(dirs[i]);
}
}
static void Main(string[] args)
{
Show("D:\\test");
Console.ReadLine();
}
}
Answer the question
In order to leave comments, you need to log in
public static void Show(string dirPath, int level)
{
...
var sb = new StringBuilder();
sb.Append('\t', level);
sb.Append(dirFiles[i]);
Console.WriteLine(sb);
...
Show(dirs[i], level + 1);
...
}
...
static void Main(string[] args)
{
Show("D:\\test", 0);
...
}
public static int level;
...
public static void Show(string dirPath)
{
...
var sb = new StringBuilder();
sb.Append('\t', level);
sb.Append(dirFiles[i]);
Console.WriteLine(sb);
...
level++;
Show(dirs[i]);
level--;
// очевидно что первый вариант будет попроще и понадежнее
...
}
...
static void Main(string[] args)
{
level = 0;
Show("D:\\test");
...
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question