0

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.

Leethemor
  • 3
  • 1
  • Find the console app in the process list, then find its parent. See [How to get parent process in .NET in managed way](https://stackoverflow.com/questions/394816/how-to-get-parent-process-in-net-in-managed-way) for details. – David Arno Jun 27 '17 at 08:23

2 Answers2

1

If you can modify the call of the console application, you can give the program name as an argument.

thi
  • 71
  • 1
  • 9
  • It is only the application beeing launched i can modify. – Leethemor Jun 27 '17 at 08:30
  • how do the other application launch yours ? Is the call hardcoded ? Because if it's not, you can always specify new arguments in the calling string. – thi Jun 27 '17 at 08:38
1

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.

Pablo notPicasso
  • 3,031
  • 3
  • 17
  • 22