123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using log4net;
- using System.Management;
- namespace Agent.Services
- {
- class DriveService
- {
- private static readonly ILog log = LogManager.GetLogger(typeof(DriveService));
- private readonly HashService _hashService = new HashService();
- private const int ByteScale = 1024;
- /// <summary>
- /// 드라이브 통계 정보
- /// </summary>
- public List<DriveStat> GetStat()
- {
- try
- {
- return DriveInfo.GetDrives()
- .Where(drive => drive.DriveType == DriveType.Fixed)
- .Select(drive => new DriveStat()
- {
- Name = drive.Name,
- TotalSize = FormatBytes(drive.TotalSize),
- FreeSize = FormatBytes(drive.AvailableFreeSpace),
- UsedSize = FormatBytes(drive.TotalSize - drive.AvailableFreeSpace),
- Label = drive.VolumeLabel
- })
- .ToList();
- }
- catch (Exception e)
- {
- log.Error(e);
- return null;
- }
- }
- /// <summary>
- /// 전체 드라이브 통계 정보
- /// </summary>
- public DriveStat GetTotalStat()
- {
- var driveStat = new DriveStat()
- {
- Label = "Total",
- TotalSize = "Load Fail",
- FreeSize = "Load Fail",
- UsedSize = "Load Fail"
- };
- try
- {
- var drives = DriveInfo.GetDrives()
- .Where(drive => drive.DriveType == DriveType.Fixed);
- if (0 == drives.Count())
- {
- log.Warn("드라이브 없음");
- return driveStat;
- }
- var totalSize = drives.Sum(drive => drive.TotalSize);
- var freeSize = drives.Sum(drive => drive.AvailableFreeSpace);
- var usedSize = totalSize - freeSize;
- driveStat.TotalSize = FormatBytes(totalSize);
- driveStat.FreeSize = FormatBytes(freeSize);
- driveStat.UsedSize = FormatBytes(usedSize);
- return driveStat;
- }
- catch (Exception e)
- {
- log.Error(e);
- return driveStat;
- }
- }
- /// <summary>
- /// Bytes -> 읽기 편한 용량
- /// </summary>
- public string FormatBytes(long bytes)
- {
- string[] orders = new string[] { "GB", "MB", "KB", "Bytes" };
- long max = (long)Math.Pow(ByteScale, orders.Length - 1);
- foreach (string order in orders)
- {
- if (bytes > max)
- {
- return string.Format("{0:##.##} {1}", decimal.Divide(bytes, max), order);
- }
- max /= ByteScale;
- }
- return "0 Bytes";
- }
- /// <summary>
- /// 디스크 시리얼
- /// </summary>
- public string GetKey()
- {
- //try
- //{
- // return _hashService.ToSHA256(Environment.MachineName);
- //}
- //catch (Exception e)
- //{
- // log.Error(e);
- // return _hashService.ToSHA256(Environment.MachineName);
- //}
- try
- {
- ManagementObjectSearcher search = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
- var management = search.Get().OfType<ManagementObject>().FirstOrDefault();
- if (null == management)
- {
- log.Warn($"드라이브 없음. NetBIOS로 대체({Environment.MachineName})");
- return _hashService.ToSHA256(Environment.MachineName);
- }
- var serial = string.Empty;
- foreach (var property in management.Properties)
- {
- if ("SerialNumber".Equals(property.Name))
- {
- serial = property.Value as string;
- break;
- }
- }
- if (null == serial || string.Empty == serial)
- {
- log.Warn($"드라이브 시리얼넘버 없음. NetBIOS로 대체({Environment.MachineName})");
- return _hashService.ToSHA256(Environment.MachineName);
- }
- return _hashService.ToSHA256(management["SerialNumber"].ToString().Trim());
- }
- catch (Exception e)
- {
- log.Error(e);
- return _hashService.ToSHA256(Environment.MachineName);
- }
- }
- }
- public partial class DriveStat
- {
- /// <summary>
- /// 드라이브 이름(EX. C:\, D:\)
- /// </summary>
- public string Name { get; set; }
- /// <summary>
- /// 전체 공간
- /// </summary>
- public string TotalSize { get; set; }
- /// <summary>
- /// 사용가능한 공간
- /// </summary>
- public string FreeSize { get; set; }
- /// <summary>
- /// 사용중인 공간
- /// </summary>
- public string UsedSize { get; set; }
- /// <summary>
- /// 별명
- /// </summary>
- public string Label { get; set; }
- }
- }
|