Added 01_filesystem_dfs for 07_trees in C# (#299)

This commit is contained in:
Ismayil Mammadov
2024-12-07 17:40:21 +04:00
committed by GitHub
parent c8ff601c52
commit 22c23551cb

View File

@@ -0,0 +1,37 @@
using System;
using System.IO;
using System.Linq;
namespace ConsoleApplication
{
public class Program
{
public static void Main(string[] args)
{
string dir = Path.Combine(Directory.GetCurrentDirectory(), "pics");
PrintNames(dir);
}
private static void PrintNames(string dir)
{
var filesAndDirs = Directory.GetFileSystemEntries(dir).OrderBy(f => f, StringComparer.OrdinalIgnoreCase);
// loop through every file and folder in the current folder
foreach (var path in filesAndDirs)
{
if (File.Exists(path))
{
// if it is a file, print out the name
Console.WriteLine(Path.GetFileName(path));
}
else
{
// if it is a folder, call this function recursively on it
// to look for files and folders
PrintNames(path);
}
}
}
}
}