Kill process by part of name - c#

Can please some one tell me how to kill a process by part of name? Example: I want to kill "explorer" but in code I want to implant to kill it by word "explor" and the rest should find out by code. Here is the code so far:
Process[] localByName = Process.GetProcessesByName("explorer");
foreach (Process p in localByName)
{
p.Kill();
}
Thank you

You could get all of the processes, then search afterwards:
var processes = Process.GetProcesses();
foreach(var p in processes.Where(proc => proc.ProcessName.IndexOf(searchString, StringComparison.CurrentCultureIgnoreCase) > -1))
p.Kill();

var localByName = Process.GetProcesses()
.Where(p => p.ProcessName.Contains("explor"));
foreach (Process p in localByName)
{
p.Kill();
}

Related

Process.GetProcesses() taking too much of CPU utilization

I'm using the following code in my windows application, I'm calling this method to find another process is running or not, the problem is, whenever I am calling this method my PC CPU utilization increase by 18-25%. Any other way to find the running process in windows? Thanks in advance.
Process[] runningProcessFromCurrentSession = Process.GetProcesses().Where(p => p.SessionId == Process.GetCurrentProcess().SessionId && p.ProcessName.Equals(TrackerAgent.Modal.Constants.Pmdriver)).ToArray();
Try to assign this expression to a variable:
var currentProcessId = Process.GetCurrentProcess().SessionId;
Process[] runningProcessFromCurrentSession =
Process.GetProcesses().Where(p => p.SessionId ==
currentProcessIdd && p.ProcessName.Equals(TrackerAgent.Modal.Constants.Pmdriver)).ToArray();
Move Process.GetCurrentProcess().SessionId to variable outside of the Where clause, otherwise Process.GetCurrentProcess() will be executed for every process found by GetProcesses, which can lead to high CPU usage:
var sessionId = Process.GetCurrentProcess().SessionId;
Process[] runningProcessFromCurrentSession = Process
.GetProcesses()
.Where(p => p.SessionId == sessionId && p.ProcessName.Equals(TrackerAgent.Modal.Constants.Pmdriver))
.ToArray();

How can I get process Id from process name? (C#)

How can I get process id from process name on C#?
I got processes using Process.GetProcessesByName("notepad");
But how can I get process id from this?
That returns an array.. because you could have 1, 4, 5 or 10 notepads open at the same time.
So, you could list them like this:
var processes = Process.GetProcessesByName("notepad");
foreach(var p in processes)
{
Console.WriteLine($"Notepad process found with ID: {p.Id}");
}
// To get NOTEPAD.EXE processes
var processes = Process.GetProcessesByName("notepad");
foreach (var process in processes)
{
Console.WriteLine("PID={0}", process.Id);
Console.WriteLine("Process Handle={0}", process.Handle);
}
Code taken from here.

List of process children in C#

In C# I am trying to get names of all opened windows, so I check processes and display their names.
I have a problem because child processes are not included, how can I include to also child processes not only one?
This is used now:
Process[] processes = Process.GetProcesses();
foreach (Process p in processes)
{
if (!String.IsNullOrEmpty(p.MainWindowTitle))
{
MessageBox.Show(p.MainWindowTitle);
}
}

Processes: receiving current open

I want to troll my brother a Little bit ;) by writing a program, which I can put in his "StartUp"-Folder. This program should scan his default system tasks so that it doesn't destroy his computer so I wrote all currently open processes in a list. Now I want to check when he opens a program (Process) is it in the list("Taskmgr" is also in the list, so you can exit the troll anytime)? If the opened Process is not in the list, kill it. If you Need any further information, please ask...
My current code is this:
void CloseProcesses()
{
Process[] arrProcesses = Process.GetProcesses();
List<string> lststrProcessNames = new List<string>();
/*Writes current running processes(+ taskmanager process) in a list*/
foreach (Process CurrentProcess in arrProcesses)
{
lststrProcessNames.Add(CurrentProcess.ProcessName);
}
lststrProcessNames.Add("taskmgr");
try
{
Process[] arrNewProcesses = Process.GetProcesses();
foreach (Process NewCurrentProcess in arrNewProcesses)
{
if (lststrProcessNames.Contains(NewCurrentProcess.ProcessName))
{
CloseProcesses();
}
else
{
NewCurrentProcess.Kill();
}
}
}
catch
{
this.Close();
}
}

Close windows application

is it possible to close a running application with another application?
I have implemented APP1.exe and APP1_UNIN.exe, I would that APP1_UNIN.exe kill running APP1.exe and uninstall it.
Is it possible?
To uninstall an application, you can start a new process and invoke msiexec.exe, and on the command line you can specify what to uninstall:
ProcessStartInfo psi;
//take your choice of which you want to use:
psi = new ProcessStartInfo("msiexec.exe", string.Format("/x {0}", "path of my msi"));
psi = new ProcessStartInfo("msiexec.exe", string.Format("/x /n {{{0}}}", "my product code"));
Process p = new Process();
p.StartInfo = psi;
p.Start();
Use System.Diagnostics.Process class:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.closemainwindow(v=VS.71).aspx
There's also a Kill method.
At least to kill the running process you can do like this:
Process[] processes = Process.GetProcesses();
foreach (Process process in processes) {
if (process.ProcessName == "APP1.exe") {
try {
process.Kill();
break;
} catch (Exception) {
//handle any exception here
}
}
}
}
Regarding uninstalling it, I'm not sure.
For closing, you can do it by killing its process. System.Diagnostics.Process
Process []pArray = Process.GetProcesses();
foreach(Process prc in pArray) {
string s = prc.ProcessName;
if (s.CompareTo("APP1") ==0) {
prc.Kill();
}
}
Yes using System.Diagnostics
You can get the process and Kill the Process.

Categories