1. HDD
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (var drive in drives)
{
switch (drive.DriveType)
{
case DriveType.Unknown:
break;
case DriveType.NoRootDirectory:
break;
case DriveType.Removable:
break;
case DriveType.Fixed:
textBox1.Text += $"Name = {drive.Name}, TotalSize = {drive.TotalSize / 1024 / 1024 / 1024}, TotalFreeSpace = {drive.TotalFreeSpace / 1024 / 1024 / 1024}" + Environment.NewLine;
break;
case DriveType.Network:
textBox1.Text += $"Name = {drive.Name}, TotalSize = {drive.TotalSize / 1024 / 1024 / 1024}, TotalFreeSpace = {drive.TotalFreeSpace / 1024 / 1024 / 1024}" + Environment.NewLine;
break;
case DriveType.CDRom:
break;
case DriveType.Ram:
break;
default:
break;
}
}
2. CPU
using System.Management;
..
using (var wmi = new ManagementObjectSearcher("select * from Win32_PerfFormattedData_PerfOS_Processor where Name != '_Total'"))
{
var cpuUsages = wmi.Get().Cast<ManagementObject>().Select(mo => (long)(ulong)mo["PercentProcessorTime"]);
var totalUsage = cpuUsages.Average();
textBox1.Text += $"cpu = {totalUsage:0.##}%" + Environment.NewLine;
}
3. RAM
using System.Management;
..
using (var wmi = new ManagementObjectSearcher("select * from Win32_OperatingSystem"))
{
var info = wmi.Get().Cast<ManagementObject>().Select(mo => new UsageInfo()
{
TotalSize = ulong.Parse(mo["TotalVisibleMemorySize"].ToString()),
FreeSize = ulong.Parse(mo["FreePhysicalMemory"].ToString()),
}).FirstOrDefault();
textBox1.Text += $"ram = {info.TotalSize / 1024}MB, {info.UsedSize / 1024}MB, {info.Usage:0.##}%, {info.FreeSize / 1024}mb" + Environment.NewLine;
}
class UsageInfo
{
/// <summary>
/// 전체 용량
/// </summary>
public ulong TotalSize { get; set; }
/// <summary>
/// 남은 용량
/// </summary>
public ulong FreeSize { get; set; }
/// <summary>
/// (readonly) 사용량
/// </summary>
public ulong UsedSize => TotalSize - FreeSize;
/// <summary>
/// (readonly) 사용률
/// </summary>
public double Usage => ((double)(UsedSize) / (double)TotalSize) * 100;
}
4. OS Version
textBox1.Text += $"Version = {Environment.OSVersion.VersionString}" + Environment.NewLine;
5. Last Boottime
using System.Management;
..
// define a select query
SelectQuery query = new SelectQuery("SELECT LastBootUpTime FROM Win32_OperatingSystem WHERE Primary = 'true'");
// create a new management object searcher and pass it
// the select query
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
// get the datetime value and set the local boot
// time variable to contain that value
foreach (ManagementObject mo in searcher.Get())
{
var dtBootTime = ManagementDateTimeConverter.ToDateTime(mo.Properties["LastBootUpTime"].Value.ToString());
// display the start time and date
textBox1.Text += dtBootTime.ToString("yyyy-MM-dd HH:mm:dd.ffff") + Environment.NewLine;
}
'C#' 카테고리의 다른 글
[C#] 윈도우 마이크 컨트롤 (AudioSwitcher, NAudio) (0) | 2021.04.06 |
---|---|
[C#] PDF 읽기 (0) | 2021.02.27 |
[C#] 공공 데이터 오픈 API 사용(제대 군인 채용 정보 얻기) (0) | 2021.01.02 |
[C#] 카카오페이지 크롤링 (0) | 2021.01.01 |
[C#] gRPC 와 LiteDB 사용 (0) | 2020.12.15 |