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;
///
/// 드라이브 통계 정보
///
public List 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;
}
}
///
/// 전체 드라이브 통계 정보
///
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;
}
}
///
/// Bytes -> 읽기 편한 용량
///
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";
}
///
/// 디스크 시리얼
///
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().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
{
///
/// 드라이브 이름(EX. C:\, D:\)
///
public string Name { get; set; }
///
/// 전체 공간
///
public string TotalSize { get; set; }
///
/// 사용가능한 공간
///
public string FreeSize { get; set; }
///
/// 사용중인 공간
///
public string UsedSize { get; set; }
///
/// 별명
///
public string Label { get; set; }
}
}