Код: Выделить всё
using System.Diagnostics;
string GetMountPoint(string path)
{
var lines = File.ReadAllLines("/proc/mounts");
string? bestMatch = null;
foreach (var line in lines)
{
var parts = line.Split(' ');
if (parts.Length < 2) continue;
var mountPoint = parts[1];
if (path.StartsWith(mountPoint) &&
(bestMatch == null || mountPoint.Length > bestMatch.Length))
{
bestMatch = mountPoint;
}
}
return bestMatch ?? "/";
}
string RunDfCommand(string path)
{
var psi = new ProcessStartInfo
{
FileName = "df",
Arguments = $"-B1 \"{path}\"",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
using var process = Process.Start(psi)!;
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
return output;
}
long ParseDfAvailableBytes(string dfOutput)
{
var lines = dfOutput.Split('\n', StringSplitOptions.RemoveEmptyEntries);
if (lines.Length < 2) return -1;
var columns = lines[1].Split(' ', StringSplitOptions.RemoveEmptyEntries);
if (columns.Length < 5) return -1;
return long.TryParse(columns[3], out long available) ? available : -1;
}
var path = "/mnt/sfroot/log";
var before = new DriveInfo(path).AvailableFreeSpace;
var after = new DriveInfo(GetMountPoint(path)).AvailableFreeSpace;
var dfOutput = ParseDfAvailableBytes(RunDfCommand(path));
Console.WriteLine($"current:\t{before}");
Console.WriteLine($"fixed:\t\t{after}");
Console.WriteLine($"df output:\t{dfOutput}");
Подробнее здесь: https://stackoverflow.com/questions/797 ... r-on-linux
Мобильная версия