我正在.Net中开发控制台应用程序,并且我想根据从cmd.exe或explorer.exe启动该应用程序的信息来对行为进行一些更改。是否可以?

最佳答案

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;

10-07 15:10