如何确定控制台应用程序是从 Powershell 还是应用程序内的标准命令行运行?
最佳答案
这样的事情可能比检查窗口标题更可靠:
using System;
using System.Diagnostics;
Process p = Process.GetCurrentProcess();
PerformanceCounter parent = new PerformanceCounter("Process", "Creating Process ID", p.ProcessName);
int ppid = (int)parent.NextValue();
if (Process.GetProcessById(ppid).ProcessName == "powershell") {
Console.WriteLine("running in PowerShell");
} else {
Console.WriteLine("not running in PowerShell");
}
[source ]
关于c# - 确定控制台应用程序是从命令行还是从 Powershell 运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27022501/