public static int DetermineSize(string path)
{
long totalSize = 0;
if (CheckIfExcluded(path) == false)
{//for the possibility that the path passed in is excluded and my code didn't catch it somehow.
Stack<string> directories = new Stack<string>(100);
if (!Directory.Exists(path))
throw new ArgumentException();
directories.Push(path);
while (directories.Count > 0)
{
string currentDirectory = directories.Pop();
if (!currentDirectory.Equals(windowsFolderPath) && !currentDirectory.Equals(programFilesFolderPath))
{
string[] subDirectories;
try
{
subDirectories = Directory.GetDirectories(currentDirectory);
}
catch (Exception)
{
continue;
}
//check for excluded directories
List<string> includedSubDirectories = new List<string>();
foreach (string directory in subDirectories)
{
if (CheckIfExcluded(directory) == false)
{
includedSubDirectories.Add(directory);
}
}
string[] files = null;
try
{
files = Directory.GetFiles(currentDirectory);
}
catch (Exception)
{
continue;
}
foreach (string file in files)
{
try
{
Delimon.Win32.IO.FileInfo fInfo = null;
bool extValid = ValidateExtension(Path.GetExtension(file));
if (!extValid)
continue;
else
fInfo = new Delimon.Win32.IO.FileInfo(file);
//Add the length of the current file to the total: The whole purpose of this method...
totalSize += fInfo.Length;
}
catch (FileNotFoundException)
{
continue;
}
}
foreach (string directoryName in includedSubDirectories)
{
directories.Push(directoryName);
}
}
}
}
//finally, calculate the amount
//change from bytes to kilobytes
totalSize /= 1024;
return Convert.ToInt32(totalSize);
}