I need to get the name of the program that launched a console application i am making in Visual studio. Any ideas on how i can do this?
I am doing this so i can see for how long robots in out company is running and when they are running.
I need to get the name of the program that launched a console application i am making in Visual studio. Any ideas on how i can do this?
I am doing this so i can see for how long robots in out company is running and when they are running.
If you can modify the call of the console application, you can give the program name as an argument.
You can get the parent process of some other process like this:
public static Process GetParentProcess(Process process)
{
string query = "SELECT ParentProcessId FROM Win32_Process WHERE ProcessId = " + process.Id;
using (ManagementObjectSearcher mos = new ManagementObjectSearcher(query))
{
foreach (ManagementObject mo in mos.Get())
{
if (mo["ParentProcessId"] != null)
{
try
{
var id = Convert.ToInt32(mo["ParentProcessId"]);
return Process.GetProcessById(id);
}
catch
{
}
}
}
}
return null;
}
Inside your console app you would use it like
var parent = GetParentProcess(Process.GetCurrentProcess());
From this you can get all information about parent process.