To add to what Dave says, there are folders in your documents that the system maintains: mostly concerned with the recycle bin.
If you check for them, you can reduce the number of exceptions, which will save time (significant time if your documents folder has a lot of subdirectories).
This may help:
private long SizeDirFolders(string dir, DirNode node)
{
if (!IsIgnorable(dir))
{
if (dir.EndsWith(":")) dir += "\\";
foreach (string path in Directory.GetDirectories(dir))
{
try
{
int last = path.LastIndexOf('\\');
if (last < 0)
{
last = 0;
}
string sname = path.Substring(last);
if (!IsIgnorable(sname))
{
DirNode dn = new DirNode(sname, 0);
lock (Root)
{
node.Add(dn);
}
SizeDir(path, dn);
}
}
catch (UnauthorizedAccessException) { }
catch (Exception ex)
{
DialogResult result = MessageBox.Show("An error occurred: " + ex.Message + "\nResults for the " + dir + "will not be complete", "Error!", MessageBoxButtons.OKCancel);
if (result == DialogResult.Cancel) return node.Size;
}
}
}
return node.Size;
}
private bool IsIgnorable(string dir)
{
if (dir.EndsWith(":System Volume Information")) return true;
if (dir.Contains(":$RECYCLE.BIN")) return true;
return false;
}
private void SizeDir(string dir, DirNode node)
{
if (!IsIgnorable(dir))
{
DirNode dn = SizeDirFiles(dir);
lock (Root)
{
node.Add(dn);
}
SizeDirFolders(dir, node);
}
}