从 "Windows 任务管理器" 进程列表中获取项目 c#

3
我正在尝试从“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*/)

        }

    }

1
System.Diagnostics.Process.GetProcesses() - Michael
为什么要重复造轮子?当“TaskManager”未打开时,您的代码将失败。使用“Process.GetProcesses()”。 - Sriram Sakthivel
请记住,以上方法很可能会在不同版本的操作系统中出现问题。 - josh poley
这比你想象的要难得多。您需要在拥有任务管理器窗口的进程中分配内存。您需要使用ReadProcessMemory和WriteProcessMemory。寻找不同的解决方案。也许使用自动化API会起作用。 - David Heffernan
@davidheff 谢谢,这正是我想要的答案。 :) 现在我知道该去哪里找了。 - user2838881
2个回答

4

我赞同评论者的观点,不要重复发明轮子。您可以按照以下方式获取流程并进行排序:

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

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

1

据我所知,user2838881想要在Windows机器上了解Windows 7中进程的排序方式。当任务管理器正在运行时,有许多方法可以对进程进行排序。由于这个原因,为了使用作为答案提供的代码,他必须找到一种通过编程对任务管理器中的进程进行排序的方法。以下代码行是实现此目的的一种方法,可以在删除进程之前添加到他的代码中。

        apiSendMessage(lhWndProcessList,LVM_SORTITEMS,0,"0")

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