判断该exe是否在运行并且强制关闭它? .NET技术 / C#
2008-06-17 10:29
543 查看
可以考虑用平台调用的方式;以及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#
.
下面贴一段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#
.
相关文章推荐
- C#.NET Form设置/取消开机自动运行,判断程序是否已经设置成开机自动启动(转载)
- c#判断应用程序是否正常运行以及输出异常关闭日志
- VBA判断ACCESS中的窗体是否存在(并且关闭和删除窗体)
- Delphi Ole操作Word,判断是否有打开的文档,并且给关闭
- C#.NET Form设置/取消开机自动运行,判断程序是否已经设置成开机自动启动
- 在VB和VB.NET程序中判断当前是否运行于开发环境中
- 在VB和VB.NET程序中判断当前是否运行于开发环境中
- C#.NET Form设置/取消开机自动运行,判断程序是否已经设置成开机自动启动
- 判断Exe文件是否正在运行的函数
- java如何判断char类型是否为空-CSDN论坛-CSDN.NET-中国最大的IT技术社区
- 【转】通过文件锁实现,程序开始运行时,先判断文件是否存在,若存在则表明该程序已经在运行了,如果不存在就用open函数创建该文件,程序退出时关闭文件并删除文件
- Inno Setup安装时判断并关闭已经运行的应用exe
- C#实现多级子目录Zip压缩解压实例 NET4.6下的UTC时间转换 [译]ASP.NET Core Web API 中使用Oracle数据库和Dapper看这篇就够了 asp.Net Core免费开源分布式异常日志收集框架Exceptionless安装配置以及简单使用图文教程 asp.net core异步进行新增操作并且需要判断某些字段是否重复的三种解决方案 .NET Core开发日志
- 通过文件锁实现,程序开始运行时,先判断文件是否存在,若存在则表明该程序已经在运行了,如果不存在就用open函数创建该文件,程序退出时关闭文件并删除文件
- 判断Exe文件是否正在运行的函数
- 通过文件锁实现,程序开始运行时,先判断文件是否存在,若存在则表明该程序已经在运行了,如果不存在就用open函数创建该文件,程序退出时关闭文件并删除文件。
- 判断Service是否关闭或运行的问题~
- 判断某个进程(例如.exe)是否存在,存在则关闭
- Delphi Ole操作Word,判断是否有打开的文档,并且给关闭
- VC 判断进程是否是以管理员权限运行,并且判断是否是用户进程而非服务进程