1.第一种使用shell命令实现:

 1 private DiskInfo LinuxGetFolderDiskInfo(string path)
 2         {
 3             DiskInfo disk = new DiskInfo();
 4             if (string.IsNullOrEmpty(path))
 5             {
 6                 return disk;
 7             }
 8             if (!path.StartsWith("/"))
 9             {
10                 path = "/" + path;
11             }
12 
13             string shellPathLine = string.Format("cd {0}", path);
14             string printLine = " awk '{print $2,$3,$4,$5}'";
15             string shellLine = string.Format("df -k {0} |", path) + printLine;
16             logger.Debug(string.Format("执行命令:{0}", shellLine));
17 
18             Process p = new Process();
19             p.StartInfo.FileName = "sh";
20             p.StartInfo.UseShellExecute = false;
21             p.StartInfo.RedirectStandardInput = true;
22             p.StartInfo.RedirectStandardOutput = true;
23             p.StartInfo.RedirectStandardError = true;
24             p.StartInfo.CreateNoWindow = true;
25             p.Start();
26             p.StandardInput.WriteLine(shellPathLine);
27             p.StandardInput.WriteLine(shellLine);
28             p.StandardInput.WriteLine("exit");
29 
30             string strResult = p.StandardOutput.ReadToEnd();
31             logger.Debug(string.Format("输出结果:{0}", strResult));
32             string[] arr = strResult.Split('\n');
33             if (arr.Length==0)
34             {
35                 return disk;
36             }
37             string[] resultArray = arr[1].TrimStart().TrimEnd().Split(' ');
38             if (resultArray==null || resultArray.Length == 0)
39             {
40                 return disk;
41             }
42 
43             disk.TotalSize = Convert.ToInt32(resultArray[0]);
44             disk.UsedSize = Convert.ToInt32(resultArray[1]);
45             disk.AvailableSize = Convert.ToInt32(resultArray[2]);
46             disk.Use = resultArray[3];
47             logger.Debug(string.Format("Linux获取目录:{0},总大小:{1},已用:{2},未用:{3},使用率:{4}", path, disk.TotalSize, disk.UsedSize, disk.AvailableSize, disk.Use));
48 
49             return disk;
50         }
View Code

相关文章:

  • 2021-11-08
  • 2021-07-10
  • 2021-11-08
  • 2021-12-26
  • 2022-12-23
  • 2022-03-07
猜你喜欢
  • 2021-07-03
  • 2022-12-23
  • 2022-12-23
  • 2021-11-17
  • 2022-02-01
  • 2022-12-23
  • 2021-12-16
相关资源
相似解决方案