Skip to content

Instantly share code, notes, and snippets.

@shawnweisfeld
Created December 16, 2018 16:17
Show Gist options
  • Save shawnweisfeld/b155a3873972b0120a7a8f51a953fcb4 to your computer and use it in GitHub Desktop.
Save shawnweisfeld/b155a3873972b0120a7a8f51a953fcb4 to your computer and use it in GitHub Desktop.
C# .net Core comparison of a loop vs. Recursion - digging through the local file system
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
var path = @"C:\Users\sweisfel\source\repos\WordPressBackup";
PrintDirRecursive(path);
//PrintDirLoop(path);
Console.ReadKey();
}
private static void PrintDirLoop(string root)
{
var folders = new Stack<string>();
folders.Push(root);
while (folders.Count > 0)
{
var currentFolder = folders.Pop();
foreach (var item in Directory.EnumerateFiles(currentFolder))
{
Console.WriteLine($"File: {item}");
}
foreach (var item in Directory.EnumerateDirectories(currentFolder))
{
folders.Push(item);
}
}
}
private static void PrintDirRecursive(string path)
{
foreach (var item in Directory.EnumerateFiles(path))
{
Console.WriteLine($"File: {item}");
}
foreach (var item in Directory.EnumerateDirectories(path))
{
PrintDirRecursive(item);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment