删除充满文件的文件夹不会立即释放 space

Deleting Folders filled with files doesn't free up space immediately

当我使用

删除文件夹时
DirectoryInfo.Delete(true)

的价值
DriveInfo.AvailableFreeSpace

几乎保持不变。例如,当我在检查 availablefreespace 时循环删除文件夹时,我最终删除了千兆字节和千兆字节的数据,而本应删除的数据少于一千兆字节。

//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);

}

有没有使用 c# 和 dotnetcore 解决这个问题的?

您正在检查的所有输入值都是整数类型。整数数学不包括数字的小数部分。

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