Search code examples
.netwindowsprocess-management

Is it possible to determine which process starts my .Net application?


I am developing console application in .Net and I want to change a behavior a little based on information that application was started from cmd.exe or from explorer.exe. Is it possible?


Solution

  • Process this_process = Process.GetCurrentProcess();
    int parent_pid = 0;
    using (ManagementObject MgmtObj = new ManagementObject("win32_process.handle='" + this_process.Id.ToString() + "'"))
    {
        MgmtObj.Get();
        parent_pid = Convert.ToInt32(MgmtObj["ParentProcessId"]);
    }
    string parent_process_name = Process.GetProcessById(parent_pid).ProcessName;