From 22c23551cb08383269bf52e678997bfb7945d5f8 Mon Sep 17 00:00:00 2001 From: Ismayil Mammadov <88452800+7empestGit@users.noreply.github.com> Date: Sat, 7 Dec 2024 17:40:21 +0400 Subject: [PATCH] Added 01_filesystem_dfs for 07_trees in C# (#299) --- 07_trees/csharp/01_filesystem_dfs/Program.cs | 37 ++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 07_trees/csharp/01_filesystem_dfs/Program.cs diff --git a/07_trees/csharp/01_filesystem_dfs/Program.cs b/07_trees/csharp/01_filesystem_dfs/Program.cs new file mode 100644 index 0000000..f2b2da1 --- /dev/null +++ b/07_trees/csharp/01_filesystem_dfs/Program.cs @@ -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); + } + } + } + } +} \ No newline at end of file