下面的代码在VS2008中测试通过
private Process p
private void button1_Click(object sender, EventArgs e)
{
if (p == null || p.HasExited)
{
//ProcessStartInfo psInfo = new ProcessStartInfo("notepad.exe")
//psInfo.WindowStyle = ProcessWindowStyle.Hidden
//隐藏
//p = Process.Start(psInfo)
p = Process.Start("notepad.exe")
}
}
private void button2_Click(object sender, EventArgs e)
{
if (p != null &&!p.HasExited)
{
p.Kill()
//如果启动的程序有显示界面,并且该进程不是隐藏启动的,也可以用下面的语句关闭该进程
//p.CloseMainWindow()
}
}
终止指定进程#include "tlhelp32.h"
void KillExe(const char* szExeName)
{
if (szExeName == NULL)
return 0
int lsr = lstrlen(szExeName)
if (lsr == 0)
return 0
PROCESSENTRY32 my
HANDLE l = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
if (((int)l) != -1)
{
my.dwSize = sizeof(my)
if (Process32First(l, &my))
{
do
{
if (lstrcmpi(szExeName, my.szExeFile + lstrlen(my.szExeFile) - lsr) == 0)
{
HANDLE hProcessHandle
hProcessHandle = OpenProcess(0x1F0FFF, true, my.th32ProcessID)
if (hProcessHandle != NULL)
{
if (TerminateProcess(hProcessHandle, 0) != NULL)
{
CloseHandle(hProcessHandle)
}
}
CloseHandle(l)
return
}
}while (Process32Next(l, &my))
}
CloseHandle(l)
}
}
调用方式
KillExe("c:\\windows\\notepad.exe")
完整或部分路径名,不区分大小写
Process[] ps = Process.GetProcesses()//获取计算机上所有进程foreach (Process p in ps)
{
if (p.ProcessName == "QQ")//判断进程名称
{
p.Kill()//停止进程
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)