删除填充文件的文件夹不会立即释放空间

时间:2017-07-31 15:39:24

标签: c# ubuntu ubuntu-14.04 .net-core ubuntu-server

使用

删除文件夹时
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?

1 个答案:

答案 0 :(得分:2)

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

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