判断该exe是否在运行并且强制关闭它? .NET技术 / C#

本文介绍了如何在.NET环境中,特别是使用C#,通过平台调用来检查并终止指定的exe进程。首先展示了C++代码片段用于获取系统中所有进程的信息,然后在C#中实现类似功能,通过`System.Diagnostics.Process`类获取进程并尝试关闭它。示例中特别关注了名为'newfax'的进程,如果找到则会强制关闭。

可以考虑用平台调用的方式;以及CreateProcess, TerminateProcess, OpenProcess等进程函数。

下面贴一段C++的代码,如果要在C#中使用,记住使用平台调用封装一下:

#include
#include
#include

BOOL GetProcessList ()
{
HANDLE hProcessSnap = NULL;
BOOL bRet = FALSE;
PROCESSENTRY32 pe32 = {0};

// Take a snapshot of all processes in the system.

hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

if (hProcessSnap == INVALID_HANDLE_VALUE)
return (FALSE);

// Fill in the size of the structure before using it.

pe32.dwSize = sizeof(PROCESSENTRY32);

// Walk the snapshot of the processes, and for each process,
// display information.

if (Process32First(hProcessSnap, &pe32))
{
DWORD dwPriorityClass;
BOOL bGotModule = FALSE;
MODULEENTRY32 me32 = {0};

do
{
bGotModule = GetProcessModule(pe32.th32ProcessID,
pe32.th32ModuleID, &me32, sizeof(MODULEENTRY32));

if (bGotModule)
{
HANDLE hProcess;

// Get the actual priority class.
hProcess = OpenProcess (PROCESS_ALL_ACCESS,
FALSE, pe32.th32ProcessID);
dwPriorityClass = GetPriorityClass (hProcess);
CloseHandle (hProcess);

// Print the process??s information.
printf( "/nPriority Class Base/t%d/n",
pe32.pcPriClassBase);
printf( "PID/t/t/t%d/n", pe32.th32ProcessID);
printf( "Thread Count/t/t%d/n", pe32.cntThreads);
printf( "Module Name/t/t%s/n", me32.szModule);
printf( "Full Path/t/t%s/n/n", me32.szExePath);
}
}
while (Process32Next(hProcessSnap, &pe32));
bRet = TRUE;
}
else
bRet = FALSE; // could not walk the list of processes

// Do not forget to clean up the snapshot object.

CloseHandle (hProcessSnap);
return (bRet);
}


///////////////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////////////////////


KillProcess(string processName)
{
System.Diagnostics.Process myproc = new System.Diagnostics.Process();
//得到所有打开的进程
try
{
foreach (Process thisproc in Process.GetProcessesByName(processName))
{
if (!thisproc.CloseMainWindow())
{
thisproc.Kill();
}
}
}
catch (Exception Exc)
{
MessageBox.Show(Exc.ToString());
}
}

private void Form1_Load(object sender, EventArgs e)
{
String tempName = "";
int begpos;
int endpos;

foreach (Process thisProc in System.Diagnostics.Process.GetProcesses())
{
tempName = thisProc.ToString();
begpos = tempName.IndexOf("(") + 1;
endpos = tempName.IndexOf(")");
tempName = tempName.Substring(begpos, endpos - begpos);
//this.listBox1.Items.Add(tempName);
if (tempName == "newfax")
{
KillProcess(tempName);
}
}
}

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//MessageBox.Show(listBox1.SelectedItem.ToString());
if (listBox1.SelectedItem.ToString() == "newfax")
{
KillProcess(listBox1.SelectedItem.ToString());
}
}

以上是判断进程里是否有newfax这个文件在运行,如有就关闭它。c#



.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值