获取C#中特定进程的磁盘使用情况

时间:2018-11-30 15:38:10

标签: c# .net

如何在C#中获取特定进程的磁盘使用率(MB / s)?

我可以这样获得CPU使用率和RAM使用率:

import threading
import csv
import subprocess
import socket
import time


def ping():
    response = subprocess.Popen(['ping.exe', device], stdout=subprocess.PIPE).communicate()[0]
    response = response.decode()
    if 'bytes=32' in response:
        status = 'Up'
        print("Ping status: %s\n" % status)
    else:
        status = 'Down'
        print("Ping status: %s\n" % status)


def nsloookup():
    name = socket.getfqdn(device)
    print("FQDN: %s" % name)


def initializefile(file):
    with open('List_of_6_Devices.csv', 'r') as f:
        return convertrows(csv.DictReader(f))


def convertrows(rows):
    return [(row['New Name']) for row in rows]


file = r"My\List_of_6_Devices.csv"
devices = initializefile(file)

if __name__ == "__main__":
    # creating thread
    _start = time.time()
    for device in devices:
        t1 = threading.Thread(target=ping)
        t2 = threading.Thread(target=nsloookup())

    # starting thread 1
        t1.start()
    # starting thread 2
        t2.start()

    # wait until thread 1 is completely executed
        t1.join()
    # wait until thread 2 is completely executed
        t2.join()

    # both threads completely executed
    print("TOTAL EXECUTION TIME", (time.time() - _start))

但是我找不到任何与磁盘使用有关的信息。

就像任务管理器中显示的一样:

et

2 个答案:

答案 0 :(得分:0)

♻️方法

如上所述here ^2

此API会告诉您I / O操作的总数以及字节总数。

您可以调用GetProcessIoCounters来获取每个进程的总体磁盘I / O数据-您需要跟踪增量并自己转换为基于时间的速率。

因此,根据this C# tutorial,您可以执行以下操作:

struct IO_COUNTERS
{
    public ULong ReadOperationCount;
    public ULong WriteOperationCount;
    public ULong OtherOperationCount;
    public ULong ReadTransferCount;
    public ULong WriteTransferCount;
    public ULong OtherTransferCount;
}

[DllImport("kernel32.dll")]
private static bool GetProcessIoCounters(IntPtr ProcessHandle, out IO_COUNTERS IoCounters);

public static void Main()
{
    IO_COUNTERS counters;
    Process[] processes = Process.GetProcesses();

    foreach(Process process In processes)
    {
        try {
            GetProcessIoCounters(process.Handle, out counters);
            console.WriteLine("\"" + process.ProcessName + " \"" + " process has read " + counters.ReadTransferCount.ToString("N0") + "bytes of data.");
        } catch (System.ComponentModel.Win32Exception ex) {
        }
    }
    console.ReadKey();
}

使用this将其转换为VB.NET

但是(发生System.ComponentModel.Win32Exception)

System.ComponentModel.Win32Exception (0x80004005): Access is denied
   at System.Diagnostics.ProcessManager.OpenProcess(Int32 processId, Int32 access, Boolean throwIfExited)
   at System.Diagnostics.Process.GetProcessHandle(Int32 access, Boolean throwIfExited)
   at System.Diagnostics.Process.OpenProcessHandle(Int32 access)
   at System.Diagnostics.Process.get_Handle()
   at taskviewerdisktest.Form1.Main() in C:\...\source\repos\taskviewerdisktest\taskviewerdisktest\Form1.vb:line 32

某些进程似乎真的很难访问..好消息是,在我看来,其中的进程并不多(250个中有15个)。.

⚠️免责声明:这更像是对“解决方案”而非“解决方案”的一种方式

♻️其他方法和参考

答案 1 :(得分:-2)

您可以使用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);
        }
    }
}