gpt4 book ai didi

c# - 删除充满文件的文件夹不会立即释放空间

转载 作者:行者123 更新时间:2023-12-04 19:01:24 25 4
gpt4 key购买 nike

当我使用删除文件夹时

DirectoryInfo.Delete(true)

的值(value)
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

关于c# - 删除充满文件的文件夹不会立即释放空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45420076/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com