When I delete a Folder using
DirectoryInfo.Delete(true)
the value for
DriveInfo.AvailableFreeSpace
stays nearly the same. For instance when I delete folders in a loop while checking availablefreespace
I end up deleting gigabytes and gigabytes of data when it should have deleted less then a gigabyte.
//Delete in secondary storage
string secondaryRoot = Path.GetPathRoot(settings.SecondaryFeatureExtractDirectory);
DriveInfo secondaryDrive = new DriveInfo(secondaryRoot);
DirectoryInfo secondaryInfo = new DirectoryInfo(settings.SecondaryFeatureExtractDirectory);
while ((secondaryDrive.TotalSize - secondaryDrive.AvailableFreeSpace) / secondaryDrive.TotalSize * 100 < settings.DriveFreePercentage)
{
logger.LogInformation("checkForCleanup before delete: {@Freespace}", secondaryDrive.AvailableFreeSpace);
secondaryThresholdPassed = true;
DirectoryInfo oldestDir = secondaryInfo.EnumerateDirectories().OrderBy(x => x.LastWriteTimeUtc).FirstOrDefault();
if (oldestDir == null) break;
oldestDir.Delete(true);
logger.LogDebug("checkForCleanup deleted: {@DirectoryName}", oldestDir.FullName);
logger.LogInformation("checkForCleanup after delete: {@Freespace}", secondaryDrive.AvailableFreeSpace);
Thread.Sleep(1);
}
Is there any was around this using c# and dotnetcore?
All of the input values you are checking are integer types. Integer math does not include decimal portions of a number.
var di = //new DriveInfo("c");
new
{
TotalSize = 255471906816L,
AvailableFreeSpace = 125059747840L
};
var ps = ((di.TotalSize - di.AvailableFreeSpace) / di.TotalSize) * 100;
Console.WriteLine(ps); // 0
var ps2 = (int)((((double)di.TotalSize - di.AvailableFreeSpace) / di.TotalSize) * 100d);
Console.WriteLine(ps2); // 51