This might be a confusing question but I have written below a Directory crawler, that will start at a root crawler, find all unique directories and then find all files and count them and add up their file size. However, the way I have it written requires going to the directory twice, one to find the directories and the next time to count the files. If/how is it possible to get all the information once?
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
HashSet<string> DirectoryHolding = new HashSet<string>();
DirectoryHolding.Add(rootDirectory);
#region All Directory Region
int DirectoryCount = 0;
int DirectoryHop = 0;
bool FindAllDirectoriesbool = true;
while (FindAllDirectoriesbool == true)
{
string[] DirectoryHolder = Directory.GetDirectories(rootDirectory);
if (DirectoryHolder.Length == 0)
{
if (DirectoryHop >= DirectoryHolding.Count())
{
FindAllDirectoriesbool = false;
}
else
{
rootDirectory = DirectoryHolding.ElementAt(DirectoryHop);
}
DirectoryHop++;
}
else
{
foreach (string DH in DirectoryHolder)
{
DirectoryHolding.Add(DH);
}
if (DirectoryHop > DirectoryHolding.Count())
{
FindAllDirectoriesbool = false;
}
rootDirectory = DirectoryHolding.ElementAt(DirectoryHop);
DirectoryHop++;
}
}
DirectoryCount = DirectoryHop - 2;
#endregion
#region File Count and Size Region
int FileCount = 0;
long FileSize = 0;
for (int i = 0; i < DirectoryHolding.Count ; i++)
{
string[] DirectoryInfo = Directory.GetFiles(DirectoryHolding.ElementAt(i));
for (int fi = 0; fi < DirectoryInfo.Length; fi++)
{
try
{
FileInfo fInfo = new FileInfo(DirectoryInfo[fi]);
FileCount++;
FileSize = FileSize + fInfo.Length;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message.ToString());
}
}
}
The stopwatch result for this is 1.38
int FileCount = 0;
long FileSize = 0;
for (int i = 0; i < DirectoryHolding.Count; i++)
{
var entries = new DirectoryInfo(DirectoryHolding.ElementAt(i)).EnumerateFileSystemInfos();
foreach (var entry in entries)
{
if ((entry.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
{
DirectoryHolding.Add(entry.FullName);
}
else
{
FileCount++;
FileSize = FileSize + new FileInfo(entry.FullName).Length;
}
}
}
the stop watch for this method is 2.01,
this makes no sense to me.
DirectoryInfo Dinfo = new DirectoryInfo(rootDirectory);
DirectoryInfo[] directories = Dinfo.GetDirectories("*.*", SearchOption.AllDirectories);
FileInfo[] finfo = Dinfo.GetFiles("*.*", SearchOption.AllDirectories);
foreach (FileInfo f in finfo)
{
FileSize = FileSize + f.Length;
}
FileCount = finfo.Length;
DirectoryCount = directories.Length;
.26 seconds i think this is the winner
if (FindAllDirectoriesbool == true)
this will work correctly:if (FindAllDirectoriesbool)
– StogyDirectory.EnumerateFiles
or recursion. – Grandiose