在C#中获取当前的CPU、RAM和磁盘使用情况

30

如何在C#代码中获取系统的CPU、RAM和磁盘驱动器使用情况?

4个回答

27

1
如何获取磁盘驱动器使用情况? - Sauron
1
通过性能计数器类。通过更改类别。 - Aamir

18

这里有一个解决方案,它将输出磁盘使用情况,即在定时器99被轮询时使用的总磁盘百分比:

 using System;
 using System.Diagnostics;
 using System.Windows;

 namespace diskpercent
 {
     public partial class MainWindow : Window
     {
         DispatcherTimer Timer99 = new DispatcherTimer();
         public MainWindow()
         {
             InitializeComponent();
             Timer99.Tick += Timer99_Tick; // don't freeze the ui
             Timer99.Interval = new TimeSpan(0, 0, 0, 0, 1024);
             Timer99.IsEnabled = true;
         }
         public PerformanceCounter myCounter =
            new PerformanceCounter("PhysicalDisk", "% Disk Time", "_Total");
         public Int32 j = 0;
         public void Timer99_Tick(System.Object sender, System.EventArgs e)

         {
                 //Console.Clear();
             j = Convert.ToInt32(myCounter.NextValue());      
                 //Console.WriteLine(j);
             textblock1.Text = j.ToString();
         }
     }
 }

以下是常见性能计数器列表:

 PerformanceCounter("Processor", "% Processor Time", "_Total");
 PerformanceCounter("Processor", "% Privileged Time", "_Total");
 PerformanceCounter("Processor", "% Interrupt Time", "_Total");
 PerformanceCounter("Processor", "% DPC Time", "_Total");
 PerformanceCounter("Memory", "Available MBytes", null);
 PerformanceCounter("Memory", "Committed Bytes", null);
 PerformanceCounter("Memory", "Commit Limit", null);
 PerformanceCounter("Memory", "% Committed Bytes In Use", null);
 PerformanceCounter("Memory", "Pool Paged Bytes", null);
 PerformanceCounter("Memory", "Pool Nonpaged Bytes", null);
 PerformanceCounter("Memory", "Cache Bytes", null);
 PerformanceCounter("Paging File", "% Usage", "_Total");
 PerformanceCounter("PhysicalDisk", "Avg. Disk Queue Length", "_Total");
 PerformanceCounter("PhysicalDisk", "Disk Read Bytes/sec", "_Total");
 PerformanceCounter("PhysicalDisk", "Disk Write Bytes/sec", "_Total");
 PerformanceCounter("PhysicalDisk", "Avg. Disk sec/Read", "_Total");
 PerformanceCounter("PhysicalDisk", "Avg. Disk sec/Write", "_Total");
 PerformanceCounter("PhysicalDisk", "% Disk Time", "_Total");
 PerformanceCounter("Process", "Handle Count", "_Total");
 PerformanceCounter("Process", "Thread Count", "_Total");
 PerformanceCounter("System", "Context Switches/sec", null);
 PerformanceCounter("System", "System Calls/sec", null);
 PerformanceCounter("System", "Processor Queue Length", null);

1
这个代码可以获取磁盘上的可用空间:PerformanceCounter("LogicalDisk", "FreeMegabytes", "C:")。 - Ben

1

驱动器

static void Main(string[] args)
{
var drives = DriveInfo.GetDrives();
foreach (DriveInfo info in drives)
{
      Console.WriteLine("Name: {0}\nSize: {1}\nDrive Format: {2}", info.Name, info.TotalSize, info.DriveFormat);
}
 Console.ReadLine();
}

0
有一个名为hardware.Info的NuGet包由Jinjinov开发。 详细信息请参见https://github.com/Jinjinov/Hardware.Info
代码示例:
hardwareInfo.RefreshAll();
//Memory
Console.WriteLine(hardwareInfo.MemoryStatus);

//CPU
foreach (var cpu in hardwareInfo.CpuList)
{
    Console.WriteLine(cpu);

    foreach (var cpuCore in cpu.CpuCoreList)
        Console.WriteLine(cpuCore);
}

//Drives
foreach (var drive in hardwareInfo.DriveList)
{
    Console.WriteLine(drive);

    foreach (var partition in drive.PartitionList)
    {
        Console.WriteLine(partition);

        foreach (var volume in partition.VolumeList)
            Console.WriteLine(volume);
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接