如何获取windows正在运行的程序

qsd12n 2009-03-25 10:51:50
问题如题
要是还能操作程序更好
希望給段 完整的代码 谢谢
...全文
239 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhizlm 2009-03-27
  • 打赏
  • 举报
回复
上面够全的。
哈哈潜伏哥 2009-03-27
  • 打赏
  • 举报
回复
如果要列出本机上所有正在运行的程序,必须调用WIN32 API非托管函数的。

Process32First函数和Process32Next函数这两个是必须的。在kernel32.dll中。.net自带的Process类还不具备全部列出运行的程序功能。

CreateToolhelp32Snapshot这个函数也需要的。

这是我刚写的代码,经测试可以正常运行。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reflection;
using System.Runtime;
using System.Runtime.InteropServices;

namespace Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

[StructLayout(LayoutKind.Sequential)]
private struct PROCESSENTRY32
{
public uint dwSize;
public uint cntUsage;
public uint th32ProcessID;
public IntPtr th32DefaultHeapID;
public uint th32ModuleID;
public uint cntThreads;
public uint th32ParentProcessID;
public int pcPriClassBase;
public uint dwFlags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x100)]
public string szExeFile;
}

[DllImport("Kernel32.dll")]
private static extern int CloseToolhelp32Snapshot(IntPtr snaph);

[DllImport("Kernel32.dll")]
private static extern int CloseHandle(IntPtr hObject);

[DllImport("Kernel32.dll")]
private static extern IntPtr CreateToolhelp32Snapshot(uint dwFlags, uint th32ProcessID);

[DllImport("Kernel32.dll")]
private static extern int Process32First(int snaph, ref PROCESSENTRY32 lppe);

[DllImport("Kernel32.dll")]
private static extern int Process32Next(IntPtr snaph, ref PROCESSENTRY32 lppe);

[DllImport("Kernel32.dll")]
private static extern bool TerminateProcess(IntPtr h, uint c);


private void button1_Click(object sender, EventArgs e)
{
IntPtr snaph = CreateToolhelp32Snapshot(2, 0);
PROCESSENTRY32 lppe = new PROCESSENTRY32();
lppe.dwSize = 0x400;
if (Process32First((int)snaph, ref lppe) == 0)
{
CloseToolhelp32Snapshot(snaph);
}
while (Process32Next(snaph, ref lppe) != 0)
{
this.listBox1.Items.Add(lppe.szExeFile);

}

}
}
}
zhu4139365 2009-03-27
  • 打赏
  • 举报
回复
2L
marvelstack 2009-03-27
  • 打赏
  • 举报
回复
下面的示例检索的信息涉及当前进程、本地计算机上运行的“记事本”的所有实例、在使用计算机别名和 IP 地址的特定计算机上运行的“记事本”的所有实例、本地计算机和远程计算机上运行的所有进程,以及本地计算机或远程计算机上使用进程 ID 的特定进程。

using System;
using System.Diagnostics;
using System.ComponentModel;

namespace MyProcessSample
{
/// <summary>
/// Shell for the sample.
/// </summary>
class MyProcess
{



void BindToRunningProcesses()
{
// Get the current process.
Process currentProcess = Process.GetCurrentProcess();


// Get all instances of Notepad running on the local
// computer.
Process [] localByName = Process.GetProcessesByName("notepad");


// Get all instances of Notepad running on the specifiec
// computer.
// 1. Using the computer alias (do not precede with "\\").
Process [] remoteByName = Process.GetProcessesByName("notepad", "myComputer");

// 2. Using an IP address to specify the machineName parameter.
Process [] ipByName = Process.GetProcessesByName("notepad", "169.0.0.0");


// Get all processes running on the local computer.
Process [] localAll = Process.GetProcesses();


// Get all processes running on the remote computer.
Process [] remoteAll = Process.GetProcesses("myComputer");


// Get a process on the local computer, using the process id.
Process localById = Process.GetProcessById(1234);


// Get a process on a remote computer, using the process id.
Process remoteById = Process.GetProcessById(2345, "myComputer");

}



static void Main()
{

MyProcess myProcess = new MyProcess();


myProcess.BindToRunningProcesses();

}
}
}
  • 打赏
  • 举报
回复
可以通过 Process获取正在运行的进程

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧