从“获取项目”;Windows任务管理器“;工艺清单c#
我正在尝试从“Windows任务管理器”进程列表中获取所有项目,并将它们添加到我的应用程序中的列表框中。我找到了在task Manager上获取进程列表句柄的代码。我有使用索引从列表中删除项目的代码(这是测试我是否有正确句柄的好方法)。但我需要c#代码来获取流程列表中的项目总数,并通过这些索引获取项目。我只想把所有的项目整理好,然后把它们添加到我的列表框中 编辑:谢谢你的评论,我会解释更多。。。从“获取项目”;Windows任务管理器“;工艺清单c#,c#,.net,winforms,pinvoke,listviewitem,C#,.net,Winforms,Pinvoke,Listviewitem,我正在尝试从“Windows任务管理器”进程列表中获取所有项目,并将它们添加到我的应用程序中的列表框中。我找到了在task Manager上获取进程列表句柄的代码。我有使用索引从列表中删除项目的代码(这是测试我是否有正确句柄的好方法)。但我需要c#代码来获取流程列表中的项目总数,并通过这些索引获取项目。我只想把所有的项目整理好,然后把它们添加到我的列表框中 编辑:谢谢你的评论,我会解释更多。。。 我不想仅仅列出进程,否则我会使用:System.Diagnostics.Process.GetPro
我不想仅仅列出进程,否则我会使用: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*/)
}
}
static Int32 LVM_FIRST=4096;
静态Int32 LVM_DELETEITEM=(LVM_FIRST+8);
静态Int32 LVM_SORTITEMS=(LVM_FIRST+48);
[DllImport(“user32.dll”,EntryPoint=“FindWindowA”)]
私有静态外部程序Int32 apiFindWindow(字符串lpClassName,字符串lpWindowName);
[DllImport(“user32.dll”,EntryPoint=“FindWindowExA”)]
私有静态外部程序Int32-apiFindWindowEx(Int32-hWnd1、Int32-hWnd2、字符串lpsz1、字符串lpsz2);
[DllImport(“user32.dll”,EntryPoint=“SendMessageA”)]
私有静态外部程序Int32 apiSendMessage(Int32 hWnd、Int32 wMsg、Int32 wParam、字符串lParam);
[DllImport(“user32.dll”,EntryPoint=“GetDesktopWindow”)]
私有静态外部程序Int32 apiGetDesktopWindow();
void GetItems()
{
Int32 lhWndParent=apiFindWindow(null,“Windows任务管理器”);
Int32 lhWndProcessList=0;
Int32 lhWndDialog=0;
对于(int i=1;(i我同意评论者的观点,不要重新发明轮子。您可以获得流程并按如下方式排序:
System.Diagnostics.Process[] myProcs = System.Diagnostics.Process.GetProcesses();
var sorted = myProcs.OrderBy(p => p.UserProcessorTime);
我同意评论者的观点,不要重新发明轮子。你可以得到流程并按如下方式排序:
System.Diagnostics.Process[] myProcs = System.Diagnostics.Process.GetProcesses();
var sorted = myProcs.OrderBy(p => p.UserProcessorTime);
据我所知,用户2838881希望了解windows 7中的进程
在windows计算机上排序。任务管理器运行时有多种方法
由于这一事实,为了使用建议的代码作为答案,他必须找到一种方法,通过编程在task manager中对流程进行排序。
实现这一点的一种方法是,在删除进程之前,可以在他的代码中添加以下代码行
apiSendMessage(lhWndProcessList,LVM_SORTITEMS,0,"0")
据我所知,用户2838881希望了解windows 7中的进程
在windows计算机上排序。任务管理器运行时有多种方法
由于这一事实,为了使用建议的代码作为答案,他必须找到一种方法,通过编程在task manager中对流程进行排序。
实现这一点的一种方法是,在删除进程之前,可以在他的代码中添加以下代码行
apiSendMessage(lhWndProcessList,LVM_SORTITEMS,0,"0")
System.Diagnostics.Process.GetProcesses()为什么要重新发明轮子?如果未打开TaskManager
,代码将失败。请使用Process.GetProcesses()
。还要记住,上述方法可能会因不同版本的操作系统而中断。这比您想象的要困难得多。您需要在拥有“任务管理器”窗口的进程中分配内存。您需要ReadProcessMemory和WriteProcessMemory。请寻找不同的解决方案。可能需要使用自动化API会有用的。@davidheff谢谢,这正是我要找的答案。:)现在我知道该去哪里找了。System.Diagnostics.Process.GetProcesses()为什么要重新发明轮子?如果没有打开TaskManager
,你的代码就会失败。使用Process.GetProcesses()
。还要记住,上述方法可能会因不同版本的操作系统而中断。这比您想象的要困难得多。您需要在拥有“任务管理器”窗口的进程中分配内存。您需要ReadProcessMemory和WriteProcessMemory。请寻找不同的解决方案。可能需要使用自动化API会有用的。@davidheff谢谢,这就是我想要的答案。:)现在我知道该去哪里找了。