biz_disk.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using HXX.Scanner.Common;
  8. namespace HXX.Scanner.Biz
  9. {
  10. /// <summary>
  11. /// 磁盘相关业务
  12. /// </summary>
  13. public class biz_disk
  14. {
  15. /// <summary>
  16. /// 获取磁盘剩余空间
  17. /// </summary>
  18. /// <param name="path"></param>
  19. /// <returns></returns>
  20. public static string getDataDirLeftSpace(string path)
  21. {
  22. string result = string.Empty;
  23. try
  24. {
  25. if (path != null && path.Length > 2)
  26. {
  27. var s = GetHardDiskFreeSpace(path.Substring(0, 1));
  28. if (s > 10)
  29. {
  30. result = (int)s + "G";
  31. }
  32. else if (s < 1)
  33. {
  34. result = (int)(s * 1000) + "M";
  35. }
  36. else
  37. {
  38. result = s.ToString("0.0") + "G";
  39. }
  40. }
  41. }
  42. catch (Exception ee)
  43. {
  44. result = "--G";
  45. LogManager.WriteLog(path + Environment.NewLine + ee.Message);
  46. }
  47. return result;
  48. }
  49. /// <summary>
  50. /// 获取磁盘剩余空间
  51. /// </summary>
  52. /// <param name="path"></param>
  53. /// <returns></returns>
  54. public static double getDataDirLeftSpace_double(string path)
  55. {
  56. return GetHardDiskFreeSpace(path.Substring(0, 1));
  57. }
  58. /// <summary>
  59. /// 获取指定驱动器的剩余空间总大小(单位为GB)
  60. /// </summary>
  61. /// <param name="str_HardDiskName">只需输入代表驱动器的字母即可 </param>
  62. private static double GetHardDiskFreeSpace(string str_HardDiskName)
  63. {
  64. double freeSpace = 0;
  65. str_HardDiskName = str_HardDiskName + ":\\";
  66. foreach (var drive in DriveInfo.GetDrives())
  67. {
  68. if (drive.Name == str_HardDiskName.ToUpper())
  69. {
  70. freeSpace = drive.TotalFreeSpace * 1.0 / (1024 * 1024 * 1024);
  71. break;
  72. }
  73. }
  74. return freeSpace;
  75. }
  76. /// <summary>
  77. /// 获取当前磁盘使用状态
  78. /// </summary>
  79. /// <returns></returns>
  80. public static ResponseEntity check_disk_status()
  81. {
  82. ResponseEntity result = new ResponseEntity();
  83. try
  84. {
  85. var dir = config_manager.Get("dataDir");
  86. if (string.IsNullOrEmpty(dir))
  87. {
  88. result.Status = 520;
  89. result.Message = "请先设置数据存放位置";
  90. }
  91. else
  92. {
  93. var vol = getDataDirLeftSpace_double(dir);
  94. if (vol < 0.5)
  95. {
  96. result.Status = 509;
  97. result.Message = "磁盘剩余空间较少";
  98. }
  99. else if (vol < 5)
  100. {
  101. result.Status = 510;
  102. result.Message = "磁盘剩余空间较少";
  103. }
  104. else
  105. {
  106. result.Status = 200;
  107. result.Message = "ok";
  108. }
  109. }
  110. }
  111. catch (Exception ee)
  112. {
  113. LogManager.WriteLog(ee);
  114. result.Status = 509;
  115. result.Message = ee.Message;
  116. }
  117. return result;
  118. }
  119. }
  120. }