DriveService.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using log4net;
  6. using System.Management;
  7. namespace Agent.Services
  8. {
  9. class DriveService
  10. {
  11. private static readonly ILog log = LogManager.GetLogger(typeof(DriveService));
  12. private readonly HashService _hashService = new HashService();
  13. private const int ByteScale = 1024;
  14. /// <summary>
  15. /// 드라이브 통계 정보
  16. /// </summary>
  17. public List<DriveStat> GetStat()
  18. {
  19. try
  20. {
  21. return DriveInfo.GetDrives()
  22. .Where(drive => drive.DriveType == DriveType.Fixed)
  23. .Select(drive => new DriveStat()
  24. {
  25. Name = drive.Name,
  26. TotalSize = FormatBytes(drive.TotalSize),
  27. FreeSize = FormatBytes(drive.AvailableFreeSpace),
  28. UsedSize = FormatBytes(drive.TotalSize - drive.AvailableFreeSpace),
  29. Label = drive.VolumeLabel
  30. })
  31. .ToList();
  32. }
  33. catch (Exception e)
  34. {
  35. log.Error(e);
  36. return null;
  37. }
  38. }
  39. /// <summary>
  40. /// 전체 드라이브 통계 정보
  41. /// </summary>
  42. public DriveStat GetTotalStat()
  43. {
  44. var driveStat = new DriveStat()
  45. {
  46. Label = "Total",
  47. TotalSize = "Load Fail",
  48. FreeSize = "Load Fail",
  49. UsedSize = "Load Fail"
  50. };
  51. try
  52. {
  53. var drives = DriveInfo.GetDrives()
  54. .Where(drive => drive.DriveType == DriveType.Fixed);
  55. if (0 == drives.Count())
  56. {
  57. log.Warn("드라이브 없음");
  58. return driveStat;
  59. }
  60. var totalSize = drives.Sum(drive => drive.TotalSize);
  61. var freeSize = drives.Sum(drive => drive.AvailableFreeSpace);
  62. var usedSize = totalSize - freeSize;
  63. driveStat.TotalSize = FormatBytes(totalSize);
  64. driveStat.FreeSize = FormatBytes(freeSize);
  65. driveStat.UsedSize = FormatBytes(usedSize);
  66. return driveStat;
  67. }
  68. catch (Exception e)
  69. {
  70. log.Error(e);
  71. return driveStat;
  72. }
  73. }
  74. /// <summary>
  75. /// Bytes -> 읽기 편한 용량
  76. /// </summary>
  77. public string FormatBytes(long bytes)
  78. {
  79. string[] orders = new string[] { "GB", "MB", "KB", "Bytes" };
  80. long max = (long)Math.Pow(ByteScale, orders.Length - 1);
  81. foreach (string order in orders)
  82. {
  83. if (bytes > max)
  84. {
  85. return string.Format("{0:##.##} {1}", decimal.Divide(bytes, max), order);
  86. }
  87. max /= ByteScale;
  88. }
  89. return "0 Bytes";
  90. }
  91. /// <summary>
  92. /// 디스크 시리얼
  93. /// </summary>
  94. public string GetKey()
  95. {
  96. //try
  97. //{
  98. // return _hashService.ToSHA256(Environment.MachineName);
  99. //}
  100. //catch (Exception e)
  101. //{
  102. // log.Error(e);
  103. // return _hashService.ToSHA256(Environment.MachineName);
  104. //}
  105. try
  106. {
  107. ManagementObjectSearcher search = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
  108. var management = search.Get().OfType<ManagementObject>().FirstOrDefault();
  109. if (null == management)
  110. {
  111. log.Warn($"드라이브 없음. NetBIOS로 대체({Environment.MachineName})");
  112. return _hashService.ToSHA256(Environment.MachineName);
  113. }
  114. var serial = string.Empty;
  115. foreach (var property in management.Properties)
  116. {
  117. if ("SerialNumber".Equals(property.Name))
  118. {
  119. serial = property.Value as string;
  120. break;
  121. }
  122. }
  123. if (null == serial || string.Empty == serial)
  124. {
  125. log.Warn($"드라이브 시리얼넘버 없음. NetBIOS로 대체({Environment.MachineName})");
  126. return _hashService.ToSHA256(Environment.MachineName);
  127. }
  128. return _hashService.ToSHA256(management["SerialNumber"].ToString().Trim());
  129. }
  130. catch (Exception e)
  131. {
  132. log.Error(e);
  133. return _hashService.ToSHA256(Environment.MachineName);
  134. }
  135. }
  136. }
  137. public partial class DriveStat
  138. {
  139. /// <summary>
  140. /// 드라이브 이름(EX. C:\, D:\)
  141. /// </summary>
  142. public string Name { get; set; }
  143. /// <summary>
  144. /// 전체 공간
  145. /// </summary>
  146. public string TotalSize { get; set; }
  147. /// <summary>
  148. /// 사용가능한 공간
  149. /// </summary>
  150. public string FreeSize { get; set; }
  151. /// <summary>
  152. /// 사용중인 공간
  153. /// </summary>
  154. public string UsedSize { get; set; }
  155. /// <summary>
  156. /// 별명
  157. /// </summary>
  158. public string Label { get; set; }
  159. }
  160. }