Kill Some Processes by .Exe File Name

Kill some processes by .exe file name

Quick Answer:

foreach (var process in Process.GetProcessesByName("whatever"))
{
process.Kill();
}

(leave off .exe from process name)

C# How can I kill a process with .exe changing name, unnamed window and cannot use PID because I need to use something that does not change?

The downloaded exe name always start with DaxSS

That's your ticket right there!

string start = "DaxSS".ToUpper();
Process P = Process.GetProcesses().Where((p) => p.ProcessName.ToUpper().StartsWith(start)).FirstOrDefault();
if (P != null)
{
P.Kill();
}

How do I kill the process exe with a specific location C#

Just use ProcessModule.FileName as per Simon's answer. Note FileName returns the full path, something that is not apparent in the post.

MSDN:

Gets the full path to the module. More...

OP:

But, when I debug his solution, I found that hModuleSnap Variable is invalid.

You shouldn't require this. Whilst Dirk's answer is fine, it's rather verbose and I feel makes excessive use of native calls.

Alternatively you can use my simplified version of Simon's answer (again no native calls):

NOTE: You must run the following code elevated

string targetProcessPath = @"c:\windows\system32\notepad.exe";
string targetProcessName = "notepad";

Process[] runningProcesses = Process.GetProcesses();
foreach (Process process in runningProcesses)
{
if (process.ProcessName == targetProcessName &&
process.MainModule != null &&
string.Compare(process.MainModule.FileName, targetProcessPath, StringComparison.InvariantCultureIgnoreCase)==0)
{
process.Kill();
}
}

The above example doesn't loop around the child modules comparing paths since Process already has a nice MainModule property that we can examine.

Now the above example isn't that thrilling but it does allow you to nuke processes called kitty running in various parts of your computer hard drive(s). You might have a c:\a\kitty.exe and d:\b\kitty.exe

How can I kill a process by name instead of PID, on Linux?

pkill firefox

More information: http://linux.about.com/library/cmd/blcmdl1_pkill.htm

How to kill a process by name?

You can use this function in order to kill a process by name:

uses
TlHelp32;

function KillTask(ExeFileName: string): Integer;
const
PROCESS_TERMINATE = $0001;
var
ContinueLoop: BOOL;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
begin
Result := 0;
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);

while Integer(ContinueLoop) <> 0 do
begin
if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) =
UpperCase(ExeFileName))) then
Result := Integer(TerminateProcess(
OpenProcess(PROCESS_TERMINATE,
BOOL(0),
FProcessEntry32.th32ProcessID),
0));
ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
end;
CloseHandle(FSnapshotHandle);
end;

Note:

  • There could be more than 1 process having the same name
  • KillTask function returns the count of the killed processes
  • The page where I've found the function says that it works on Windows 9x/ME/2000/XP.
  • I've personally tested it on Windows 7/10

Killing processes from list of executable names in PowerShell

Try adding the following just after the call to GetExecutableNames

%{ $_.Path }

Full answer

Get-Executable-Names 
| where { $_ -match ".exe" }
| %{ $_.Path }
| %{ $_ -replace ".exe" }
| %{ ps $_ }
| kill

How to kill a process based on its image / file path?

Using WMIC, as suggested in the comments by Squashman, you can do it as a single command:

WMIC Process Where "ExecutablePath='C:\\path\\to\\exe\\app.exe'" Call Terminate

Kill process in batch file that matches certain name and user

Here is the code I use now, which works great:

    :PRVARCHSTART

tasklist /FI "IMAGENAME eq %TFProName%" | find /I "%TFProName%" >nul

IF ERRORLEVEL 1 (
echo **** %TIME% - Another archive process is not running...checking for an import/export process... **** >> "%logfilefolder%\%logfilename%"
goto PRVARCHEND
) ELSE (
echo **** %TIME% - Another archive is running...please wait...checking again... **** >> "%logfilefolder%\%logfilename%"
PING 1.1.1.1 -n 1 -w 5000 >nul
powershell "& '%scriptdirectory%\KillTFProAdmin.ps1'"
goto PRVARCHSTART
)
:PRVARCHEND

Kill all processes with a certain name

The error message means that Exec can't find pskill.exe, so the executable most likely isn't located in the %PATH% or the current working directory. You could mitigate that by specifying the full path to the executable.

However, the objects returned from querying Win32_Process have a Terminate method. I'd recommend using that instead of shelling out:

For Each Process in ProcessList
Process.Terminate
Next


Related Topics



Leave a reply



Submit