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.

1

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

It is only the application beeing launched i can modify.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.