DirectoryInfo di = new DirectoryInfo(lPath);
DirectoryInfo dest = new DirectoryInfo(lPath9);
if (!dest.Exists) dest.Create(di.GetAccessControl());
string mapDirName = di.FullName;
di.Delete(true);
Thread.Sleep(20); // let the process wait a bit
dest.MoveTo(mapDirName);
Thread.Sleep(20); // let the process wait a bit
The above code works most of the time. However, sometime some sub-directories are missing after dest is renamed to di.
I think it is because the rename has started before the delete is complete.
I can add a while loop to check for the existence of di before I rename.
Such as,
int i=0;
While (di.Exists && i < 10) {
Thread.Sleep(10000);
i++;
}
Still it would only wait 10000*10 milliseconds. There is no sure way of doing it without getting into an infinite loop.
Instead of waiting an arbitrary amount of time before moving the dest directory, why not loop through the files in the di directory?