I have a requirement of killing an exe if I find an error in the log file and restart it again. The main problem here is, I cannot just delete the exe based on the name as I have the same exe running from different folders, such as D:\A\A1.exe and D:\B\A1.exe. I only want to delete the exe from "A" folder.
I have tried to follow Dirk Vollmar's Solution (https://stackoverflow.com/a/2238316/5159431).
to this question - C# Process Killing
But, when I debug his solution, I found that hModuleSnap Variable is invalid.
Update - 1
As Micky suggested, I have used Simon's Answer. It does kill the exe (Thanks for that). However, I am getting an error saying "System.ComponentModel.Win32Exception: Only part of a ReadProcessMemory or WriterocessMemory request was completed".
Here is the example code.
string path1 = @"F:\Software\Application\Runner.exe";
try
{
Process[] runningProcesses = Process.GetProcesses();
foreach (Process process in runningProcesses)
{
// now check the modules of the process
foreach (ProcessModule module in process.Modules)
{
if (module.FileName.Equals(path1))
{
process.Kill();
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.ReadLine();
}