C#获取Linux磁盘剩余空间
/// <summary> /// Linux获取磁盘剩余空间,单位byte /// </summary> /// <param name="path">例:/home</param> /// <returns></returns> public long LinuxGetFolderDiskInfo(string path) { try { if (string.IsNullOrEmpty(path)) { return 0; } if (!path.StartsWith("/")) { path = "/" + path; } string shellPathLine = string.Format("cd {0}", path); string shellLine = string.Format("df {0}", path); 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('\n'); if (arr.Length == 0) { return 0; } string strArray = arr[1].TrimStart().TrimEnd(); //把字符串中多个空格合并成一个(overlay 20961280 6256512 14704768 30%) while (strArray.IndexOf(" ") > 0) { strArray = strArray.Replace(" ", " "); } string[] resultArray = strArray.Split(' '); if (resultArray == null || resultArray.Length == 0) { return 0; } //读取可用空间,单位KB long availableSize = Convert.ToInt32(resultArray[3]); //KB => byte availableSize *= 1024; return availableSize; } catch (Exception ex) { throw ex; } }