gpt4 book ai didi

c# - 从 "Windows Task Manager"进程列表中获取项目 c#

转载 作者:行者123 更新时间:2023-11-30 20:54:26 25 4
gpt4 key购买 nike

我正在尝试从“Windows 任务管理器”进程列表中获取所有项目,并将它们添加到我的应用程序的列表框中。我找到了代码来获取任务管理器上进程列表的句柄。我有代码使用它的索引从列表中删除一个项目(这是测试我是否有正确句柄的好方法)。但我需要 c# 代码来获取进程列表中的项目总数并通过索引获取项目。我满足于简单地整理所有项目并将它们添加到我的列表框中。

编辑:感谢您的评论,我会解释更多...我不想只列出进程,否则我会使用:System.Diagnostics.Process.GetProcesses();我想让进程按照它们在任务管理器中的排序方式进行排序。任务管理器有很多方法来排序它的进程。它提供了大约 31 种不同的方法。例如:图像名称、PID、用户名、CPU 使用率等。我的目标是按照任务管理器中的排序顺序获取进程。

    static Int32 LVM_FIRST = 4096;
static Int32 LVM_DELETEITEM = (LVM_FIRST + 8);
static Int32 LVM_SORTITEMS = (LVM_FIRST + 48);

[DllImport("user32.dll", EntryPoint = "FindWindowA")]
private static extern Int32 apiFindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll", EntryPoint = "FindWindowExA")]
private static extern Int32 apiFindWindowEx(Int32 hWnd1, Int32 hWnd2, string lpsz1, string lpsz2);

[DllImport("user32.dll", EntryPoint = "SendMessageA")]
private static extern Int32 apiSendMessage(Int32 hWnd, Int32 wMsg, Int32 wParam, string lParam);

[DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
private static extern Int32 apiGetDesktopWindow();


void GetItems()
{
Int32 lhWndParent = apiFindWindow(null, "Windows Task Manager");
Int32 lhWndProcessList = 0;
Int32 lhWndDialog = 0;

for (int i = 1; (i <= 7); i++)
{
// Loop through all seven child windows, for handle to the listview
lhWndDialog = apiFindWindowEx(lhWndParent, lhWndDialog, null, null);
if ((lhWndProcessList == 0))
{
lhWndProcessList = apiFindWindowEx(lhWndDialog, 0, "SysListView32", "Processes");
}
}


/* This code removes the first item in the Task Manager Processes list:
* apiSendMessage(lhWndProcessList, LVM_DELETEITEM, 0, "0");
* note that the 3rd paramiter: 0, is the index of the item to delete.
* I put it here in comments because I thought there would be
* something similar to get the name*/

listBox1.Items.Clear();

int TotalItemCount = /*Total item count in Task Manager Processes list*/;
for (int i = 0; i < TotalItemCount; i++)
{
listBox1.Items.Add(/*Get item in Task Manager Processes list by index: i*/)

}

}

最佳答案

我同意评论者不要重新发明轮子。您可以像这样获取流程和排序:

System.Diagnostics.Process[] myProcs = System.Diagnostics.Process.GetProcesses();

var sorted = myProcs.OrderBy(p => p.UserProcessorTime);

关于c# - 从 "Windows Task Manager"进程列表中获取项目 c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19138741/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com