Get Directory Size Recursively
public static long GetDirectorySize(string p)
{
string[] a = Directory.GetFiles(p, "*.*");
long b = 0;
foreach (string name in a)
{
FileInfo info = new FileInfo(name);
b += info.Length;
}
return b;
}
public static long GetDirectorySize(string b, Boolean recurse)
{
if (!recurse)
return GetDirectorySize(b);
long result = 0;
Stack<string> stack = new Stack<string>();
stack.Push(b);
while (stack.Count > 0)
{
string dir = stack.Pop();
try
{
result += GetDirectorySize(dir);
foreach (string dn in Directory.GetDirectories(dir))
{
stack.Push(dn);
}
}
catch { }
}
return result;
}
Kommentare zum Snippet