List of process children in C# - 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);
}
}

Related

Closing more than one Autohotkeys applications

I have 4 applications and I have to use 4 different autohotkey exe applications for always on Top future. However, I would like to close all of them or kill their process.
I tried using ESC::ExitApp, Process.Kill() function as well, but only one of them is closing.
Here is my code:
#SingleInstance Force
WinSet, AlwaysOnTop, Toggle, Bot
Winset, Style, -0xC00000, Bot
^!p::Pause
*ESC::ExitApp
return
Other applications are the same but different Windows Title name.
Process[] bottom = Process.GetProcessesByName("bottom");
//MessageBox.Show(workers.Length.ToString());
foreach (Process bot in bottom)
{
bot.Kill();
bot.WaitForExit();
bot.Dispose();
}
Process[] left = Process.GetProcessesByName("left");
//MessageBox.Show(workers.Length.ToString());
foreach (Process l in left)
{
l.Kill();
l.WaitForExit();
l.Dispose();
}
Process[] right = Process.GetProcessesByName("test1");
//MessageBox.Show(workers.Length.ToString());
foreach (Process r in right)
{
r.Kill();
r.WaitForExit();
r.Dispose();
}
Process[] top = Process.GetProcessesByName("top");
//MessageBox.Show(workers.Length.ToString());
foreach (Process t in top)
{
t.Kill();
t.WaitForExit();
t.Dispose();
}
SendKeys.Send("{ESC}");
According to above code, I tried to kill 4 apps but only one is closing. Plus I cannot see in TaskManager running autohotkeys.exe files.Is there any way
to turn off or close all autohotkeys.exe files? Thank you in advance.
You can use the WMI from windows to do that.
ESC::
Query := "Select ProcessId, CommandLine from Win32_Process where name = 'Autohotkey.exe'"
for process in ComObjGet("winmgmts:").ExecQuery(Query, "WQL")
process, close, % process.ProcessId
Return

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.

Closing a program running under another users profile using C#

I made a program that opens COM6. The program starts when the user logs on.
If another user logs on, while the first user is still logged in, the program crazes because the COM is already open.
I found this code, which I thought could solve the problem. The code was meant to close all other application with the same name, but apparently, it does not work, when the other app is running under another user. Have anybody got any solution for this ?
void CloseAllButMe()
{
Process[] processes;
Process self = Process.GetCurrentProcess();
processes = Process.GetProcessesByName(self.ProcessName);
foreach (Process p in processes)
{
if (self.Id != p.Id) p.CloseMainWindow();
}
}
You can use the methods Process.Kill to stop a process. Calling Kill will immediately stop the process and could cause a loss of work.
Here is a code sample for killing Calculator:
public static void KillPaint()
{
System.Diagnostics.Process[] procs = null;
try
{
procs = Process.GetProcessesByName("calc");
Process mspaintProc = procs[0];
if (!mspaintProc.HasExited)
{
mspaintProc.Kill();
}
}
finally
{
if (procs != null)
{
foreach (Process p in procs)
{
p.Dispose();
}
}
}
}

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();
}
}

Do I need to worry about Process in foreach loop

Here is the piece of code, which run through all the process and when It finds the right process, code sends the message. My question is what happened to the 'proc', how to dispose that process.
//get all other (possible) running instances
Process[] processes = Process.GetProcesses();
foreach (Process proc in processes)
{
if (proc.ProcessName.ToLower() == ProcessName.ToLower())
{
SendMessage(proc.MainWindowHandle, (uint)Message, IntPtr.Zero, IntPtr.Zero);
}
}
Thanks in advance,
Harsha
To make sure all resoucers are freed as early as possible, call Dispose on the process, when you no longer need it.
//get all other (possible) running instances
Process[] processes = Process.GetProcesses();
try
{
foreach (Process proc in processes)
{
// use proc
}
}
finally
{
foreach (Process proc in processes)
proc.Dispose();
processes = null;
}
In general terms you don't need to worry about disposing or deallocating objects, unless the object implements the IDisposable interface. If it does you should either call the Dispose() method on it manually when you're finished, or wrap with a using statement to have it called automatically:
using (var disposableObject = new DisposableType())
{
// do work with disposableObject
}
IF you are looping to find your won process then you could try something like:
Process.GetCurrentProcess();
In any case I would change it to:
foreach (Process proc in Process.GetProcesses())
{
if (proc.ProcessName.ToLower() == ProcessName.ToLower())
{
SendMessage(proc.MainWindowHandle, (uint)Message, IntPtr.Zero, IntPtr.Zero);
}
}
That way no variable will refernce the "GetProcesses" and the GC would eventually handle it.
The variable proc is local to the foreach loop so once the loop completes, it will automatically be garbage collected.

Categories