-
Notifications
You must be signed in to change notification settings - Fork 9
Traversing Directories
Dirkster99 edited this page Mar 12, 2019
·
1 revision
The code on this page shows different directory traversal strategies applied to the directory structure of the zip file Diff.Net.zip with the original code contribution from Bill Menees.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Program
{
static void Main(string[] args)
{
string rootDirPath = @"<...>\C#\20 Diff.Net_Sources\10 Diff.Net First Version\";
var rootDir = new DirectoryInfo(rootDirPath);
//LevelOrderTraversal(rootDir, rootDirPath);
//PreOrder(rootDir, rootDirPath);
nonRecursivePostOrder(rootDir, rootDirPath);
Console.WriteLine("\n\nPress any key...");
Console.ReadKey();
}
public static void LevelOrderTraversal(DirectoryInfo root, string rootDirPath)
{
Queue<Tuple<int, DirectoryInfo>> queue = new Queue<Tuple<int, DirectoryInfo>>();
if (root != null)
queue.Enqueue(new Tuple<int, DirectoryInfo>(0, root));
while (queue.Count > 0)
{
var queueItem = queue.Dequeue();
int iLevel = queueItem.Item1;
DirectoryInfo current = queueItem.Item2;
string subPath = current.FullName.Substring(rootDirPath.Length);
Console.WriteLine(string.Format("{0,4} - {1}", iLevel, subPath)); // Process the node
foreach (var item in current.GetDirectories())
queue.Enqueue(new Tuple<int, DirectoryInfo>(iLevel + 1, item));
}
}
/***
Result:
0 -
1 - .vs
1 - 00 Docs
1 - Diff.Net
1 - Libs
2 - .vs\Diff.Net
2 - Diff.Net\bin
2 - Diff.Net\Images
2 - Diff.Net\obj
2 - Diff.Net\Properties
2 - Libs\Menees.Common
2 - Libs\Menees.Diffs
2 - Libs\Menees.Diffs.Controls
2 - Libs\Menees.Windows.Forms
3 - .vs\Diff.Net\v15
3 - Diff.Net\bin\Debug
3 - Diff.Net\obj\Debug
3 - Libs\Menees.Common\Bin
3 - Libs\Menees.Common\Collections
3 - Libs\Menees.Common\Diagnostics
3 - Libs\Menees.Common\obj
3 - Libs\Menees.Common\Properties
3 - Libs\Menees.Common\Shell
3 - Libs\Menees.Diffs\bin
3 - Libs\Menees.Diffs\Docs
3 - Libs\Menees.Diffs\obj
3 - Libs\Menees.Diffs\Properties
3 - Libs\Menees.Diffs.Controls\bin
3 - Libs\Menees.Diffs.Controls\Images
3 - Libs\Menees.Diffs.Controls\obj
3 - Libs\Menees.Diffs.Controls\Properties
3 - Libs\Menees.Windows.Forms\Bin
3 - Libs\Menees.Windows.Forms\Images
3 - Libs\Menees.Windows.Forms\obj
3 - Libs\Menees.Windows.Forms\Properties
4 - .vs\Diff.Net\v15\Server
4 - Diff.Net\obj\Debug\TempPE
4 - Libs\Menees.Common\Bin\Debug
4 - Libs\Menees.Common\obj\Debug
4 - Libs\Menees.Diffs\bin\Debug
4 - Libs\Menees.Diffs\obj\Debug
4 - Libs\Menees.Diffs.Controls\bin\Debug
4 - Libs\Menees.Diffs.Controls\obj\Debug
4 - Libs\Menees.Windows.Forms\Bin\Debug
4 - Libs\Menees.Windows.Forms\obj\Debug
5 - .vs\Diff.Net\v15\Server\sqlite3
5 - Libs\Menees.Common\obj\Debug\TempPE
5 - Libs\Menees.Diffs\obj\Debug\TempPE
5 - Libs\Menees.Diffs.Controls\obj\Debug\TempPE
5 - Libs\Menees.Windows.Forms\obj\Debug\TempPE
Press any key...
***/
public static void PreOrder(DirectoryInfo root, string rootDirPath)
{
var stack = new Stack<DirectoryInfo>();
stack.Push(root);
while (stack.Count > 0)
{
var current = stack.Pop();
string subPath = current.FullName.Substring(rootDirPath.Length);
Console.WriteLine(string.Format("'{0}'", subPath)); // Process the node
var subdirs = current.GetDirectories();
for (int i = subdirs.Length - 1; i >= 0; i--)
{
stack.Push(subdirs[i]);
}
}
}
/***
''
'.vs'
'.vs\Diff.Net'
'.vs\Diff.Net\v15'
'.vs\Diff.Net\v15\Server'
'.vs\Diff.Net\v15\Server\sqlite3'
'00 Docs'
'Diff.Net'
'Diff.Net\bin'
'Diff.Net\bin\Debug'
'Diff.Net\Images'
'Diff.Net\obj'
'Diff.Net\obj\Debug'
'Diff.Net\obj\Debug\TempPE'
'Diff.Net\Properties'
'Libs'
'Libs\Menees.Common'
'Libs\Menees.Common\Bin'
'Libs\Menees.Common\Bin\Debug'
'Libs\Menees.Common\Collections'
'Libs\Menees.Common\Diagnostics'
'Libs\Menees.Common\obj'
'Libs\Menees.Common\obj\Debug'
'Libs\Menees.Common\obj\Debug\TempPE'
'Libs\Menees.Common\Properties'
'Libs\Menees.Common\Shell'
'Libs\Menees.Diffs'
'Libs\Menees.Diffs\bin'
'Libs\Menees.Diffs\bin\Debug'
'Libs\Menees.Diffs\Docs'
'Libs\Menees.Diffs\obj'
'Libs\Menees.Diffs\obj\Debug'
'Libs\Menees.Diffs\obj\Debug\TempPE'
'Libs\Menees.Diffs\Properties'
'Libs\Menees.Diffs.Controls'
'Libs\Menees.Diffs.Controls\bin'
'Libs\Menees.Diffs.Controls\bin\Debug'
'Libs\Menees.Diffs.Controls\Images'
'Libs\Menees.Diffs.Controls\obj'
'Libs\Menees.Diffs.Controls\obj\Debug'
'Libs\Menees.Diffs.Controls\obj\Debug\TempPE'
'Libs\Menees.Diffs.Controls\Properties'
'Libs\Menees.Windows.Forms'
'Libs\Menees.Windows.Forms\Bin'
'Libs\Menees.Windows.Forms\Bin\Debug'
'Libs\Menees.Windows.Forms\Images'
'Libs\Menees.Windows.Forms\obj'
'Libs\Menees.Windows.Forms\obj\Debug'
'Libs\Menees.Windows.Forms\obj\Debug\TempPE'
'Libs\Menees.Windows.Forms\Properties'
Press any key...
***/
public static void nonRecursivePostOrder(DirectoryInfo root, string rootDirPath)
{
var toVisit = new Stack<DirectoryInfo>();
var visitedAncestors = new Stack<DirectoryInfo>();
toVisit.Push(root);
while (toVisit.Count > 0)
{
var node = toVisit.Peek();
if (node.GetDirectories().Length > 0)
{
if (PeekOrDefault(visitedAncestors) != node)
{
visitedAncestors.Push(node);
PushReverse(toVisit, node.GetDirectories().ToList());
continue;
}
visitedAncestors.Pop();
}
string subPath = node.FullName.Substring(rootDirPath.Length);
Console.WriteLine(string.Format("'{0}'", subPath)); // Process the node
toVisit.Pop();
}
}
private static DirectoryInfo PeekOrDefault(Stack<DirectoryInfo> s)
{
return s.Count == 0 ? null : s.Peek();
}
private static void PushReverse(Stack<DirectoryInfo> s, List<DirectoryInfo> list)
{
foreach (var l in list.ToArray().Reverse())
{
s.Push(l);
}
}
/***
'.vs\Diff.Net\v15\Server\sqlite3'
'.vs\Diff.Net\v15\Server'
'.vs\Diff.Net\v15'
'.vs\Diff.Net'
'.vs'
'00 Docs'
'Diff.Net\bin\Debug'
'Diff.Net\bin'
'Diff.Net\Images'
'Diff.Net\obj\Debug\TempPE'
'Diff.Net\obj\Debug'
'Diff.Net\obj'
'Diff.Net\Properties'
'Diff.Net'
'Libs\Menees.Common\Bin\Debug'
'Libs\Menees.Common\Bin'
'Libs\Menees.Common\Collections'
'Libs\Menees.Common\Diagnostics'
'Libs\Menees.Common\obj\Debug\TempPE'
'Libs\Menees.Common\obj\Debug'
'Libs\Menees.Common\obj'
'Libs\Menees.Common\Properties'
'Libs\Menees.Common\Shell'
'Libs\Menees.Common'
'Libs\Menees.Diffs\bin\Debug'
'Libs\Menees.Diffs\bin'
'Libs\Menees.Diffs\Docs'
'Libs\Menees.Diffs\obj\Debug\TempPE'
'Libs\Menees.Diffs\obj\Debug'
'Libs\Menees.Diffs\obj'
'Libs\Menees.Diffs\Properties'
'Libs\Menees.Diffs'
'Libs\Menees.Diffs.Controls\bin\Debug'
'Libs\Menees.Diffs.Controls\bin'
'Libs\Menees.Diffs.Controls\Images'
'Libs\Menees.Diffs.Controls\obj\Debug\TempPE'
'Libs\Menees.Diffs.Controls\obj\Debug'
'Libs\Menees.Diffs.Controls\obj'
'Libs\Menees.Diffs.Controls\Properties'
'Libs\Menees.Diffs.Controls'
'Libs\Menees.Windows.Forms\Bin\Debug'
'Libs\Menees.Windows.Forms\Bin'
'Libs\Menees.Windows.Forms\Images'
'Libs\Menees.Windows.Forms\obj\Debug\TempPE'
'Libs\Menees.Windows.Forms\obj\Debug'
'Libs\Menees.Windows.Forms\obj'
'Libs\Menees.Windows.Forms\Properties'
'Libs\Menees.Windows.Forms'
'Libs'
''
Press any key...
***/
}