Reputation: 149
No, this is not Malware. I'm trying to make a PC helper tool that kills all tasks that are non-Windows processes, but in the process, it kills itself.
It's a console application, so I tried disincluding cmd.exe to kill, but it still kills my program.
Is there a way kind of like this?
if (process.ToString == this.ExecutableName)
Upvotes: 2
Views: 4190
Reputation: 11
I think you should not use (.exe) in your process name and you must use (using System.Linq;
) not to make any error while using (.Any()
).
if (Process.GetProcessesByName("YourProcessNameGoesHere").Any())
{
Process MyCurentProcess = Process.GetCurrentProcess();
foreach (Process process in Process.GetProcessesByName("YourProcessNameGoesHere").Where(process => process.Id != MyCurentProcess.Id))
{
process.Kill();
}
}
Upvotes: 0
Reputation: 6562
List<string> safeProcs = new List<string>() { "App1", "App2", "App3" };
List<Process> procs = Process.GetProcesses()
.Where(p => !p.Equals(Process.GetCurrentProcess()) && !safeProcs.Contains(p.ProcessName))
.ToList();
procs.ForEach(p => p.Kill());
Upvotes: 0
Reputation: 288
You can use this procedure:
private void KillProcess(string _process)
{
Process[] procs = Process.GetProcesses();
foreach (Process proc in procs)
{
if (proc.ProcessName == _process)
{
proc.Kill();
proc.WaitForExit();
}
}
}
then:
KillProcess(this.ExecutableName);
Upvotes: -1
Reputation: 74355
It doesn't get much easier than
Process self = Process.GetCurrentProcess() ;
foreach( Process p in Process.GetProcesses().Where( p => p.Id != self.Id ) )
{
p.Kill() ;
}
If you need to worry about your parent process (so you don't kill the command shell that launched your process, the answers to the question "How can I get the PID of the parent process of my application" should guide you.
Upvotes: 3
Reputation: 723
If you run the application from the solution folder, your process will be easily identifiable in the task manager eg: consoleApplication1.exe
You can get the name of the process by:
var cb = Assembly.GetExecutingAssembly().CodeBase;
var processName = Path.GetFileName(cb);
You will see that in your list of processes in task manager, the name of the process is different if you are debugging and you have "Enable the visual studio hosting process" checked. In this instance the process name would be
consoleApplication1.vshost.exe
You can uncheck this option by right clicking on your project and clicking on "Properties" and then selecting "Debug". You should see a checkbox saying "Enable the visual studio hosting process". Deselect the check box.
Your process name will then be
consoleApplication1.exe
Upvotes: 0