public DiskInfo LinuxDisk(string path) { DiskInfo disk = new DiskInfo(); if (string.IsNullOrEmpty(path)) { return disk; } if (!path.StartsWith("/")) { path = "/" + path; } string shellPathLine = string.Format("cd {0}", path); string printLine = " awk '{print $2,$3,$4,$5}'"; string shellLine = string.Format("df -k {0} |", path) + printLine; Process p = new Process(); p.StartInfo.FileName = "sh"; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.CreateNoWindow = true; p.Start(); p.StandardInput.WriteLine(shellPathLine); p.StandardInput.WriteLine(shellLine); p.StandardInput.WriteLine("exit"); string strResult = p.StandardOutput.ReadToEnd(); string[] arr = strResult.Split(' '); if (arr.Length == 0) { return disk; } string[] resultArray = arr[1].TrimStart().TrimEnd().Split(' '); if (resultArray == null || resultArray.Length == 0) { return disk; } disk.TotalSize = Convert.ToInt32(resultArray[0]); disk.UsedSize = Convert.ToInt32(resultArray[1]); disk.AvailableSize = Convert.ToInt32(resultArray[2]); disk.Use = resultArray[3]; logger.Info(string.Format("Linux获取目录:{0},总大小:{1},已用:{2},未用:{3},使用率:{4}", path, disk.TotalSize, disk.UsedSize, disk.AvailableSize, disk.Use)); return disk; }
public class DiskInfo { public long TotalSize { get; set; } public long UsedSize { get; set; } public long AvailableSize { get; set; } public string Use { get; set; } }