본문 바로가기
C#

[C#] CPU, RAM, HDD 사용량 및 OS Version, Last boottime 확인

by Jcoder 2021. 2. 10.

결과

 

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;
         }