2016-09-16 7 views
4

現在、.NET COREを使用しているC#Webアプリケーションで現在のCPU/RAM /ディスク使用量を取得する方法を探しています。.NET COREを使用してC#Webアプリケーションで現在のCPU/RAM /ディスク使用量を取得する方法は?

CPUとRAMの使用については、System.DiagnosticsのPerformanceCounterクラスを使用します。 これらのコードです:ディスクの使用状況については

PerformanceCounter cpuCounter; 
PerformanceCounter ramCounter; 

cpuCounter = new PerformanceCounter(); 

cpuCounter.CategoryName = "Processor"; 
cpuCounter.CounterName = "% Processor Time"; 
cpuCounter.InstanceName = "_Total"; 

ramCounter = new PerformanceCounter("Memory", "Available MBytes"); 


public string getCurrentCpuUsage(){ 
     cpuCounter.NextValue()+"%"; 
} 

public string getAvailableRAM(){ 
     ramCounter.NextValue()+"MB"; 
} 

、私がDriveInfoクラスを使用します。これらは、コードです:

using System; 
using System.IO; 

class Info { 
public static void Main() { 
    DriveInfo[] drives = DriveInfo.GetDrives(); 
    foreach (DriveInfo drive in drives) { 
     //There are more attributes you can use. 
     //Check the MSDN link for a complete example. 
     Console.WriteLine(drive.Name); 
     if (drive.IsReady) Console.WriteLine(drive.TotalSize); 
    } 
    } 
} 

は、残念ながら、.NETのコアは、したがって、上記のコードが動作しない、DriveInfoとPerformanceCounterクラスをサポートしていません。

.NET COREを使用してC#Webアプリケーションで現在のCPU/RAM /ディスクの使用状況をどのように取得できるかを知っている人はいますか?

+1

System.IO.FileSystem.DriveInfoパッケージを追加することによって、コアのために利用可能です9376 – thepirat000

+0

P/Invokeは.NETコアですか?私はcoreclrで100%立ち上がったわけではありませんが、P/Invokeを持っていてネイティブのWindowsライブラリを呼び出すことができるなら、それを行う方法があります。 – Thumper

答えて

1

プロセッサの情報がSystem.Diagnosticsを経由して提供されています:https://github.com/dotnet/corefx/issues/:

var proc = Process.GetCurrentProcess(); 
var mem = proc.WorkingSet64; 
var cpu = proc.TotalProcessorTime; 
Console.WriteLine("My process used working set {0:n3} K of working set and CPU {1:n} msec", 
    mem/1024.0, cpu.TotalMilliseconds); 

DriveInfoこの未解決の問題を参照してください

関連する問題