以编程方式获取powershell.exe的完整路径
To get full path of powershell.exe programmatically
我需要获得Powershell.exe的完整路径(绝对路径)才能在代码中运行Powershell脚本。有人能向我推荐任何库或内置方法来做到这一点吗。
我在windows.h中尝试了boost文件系统的absolute()方法和getFUllPathName()方法。但我所能得到的只是我项目的当前工作目录。
或者(如果您不喜欢依赖环境变量),您也可以查询注册表。Powershell在安装时会写入一个FriendlyTypeName值,在我的机器上它是
@"%systemroot%\system32\windowspowershell \v1.0\powershell.exe",-107
其包含路径。
注册表项为HKEY_CLASSES_ROOTMicrosoft.PowerShellConsole.1FriendlyTypeName
使用Windows API查询注册表。
解决方案1:Powershell.exe很可能在您的路径中,所以您可以在C++代码中使用它。
system("powershell.exe -Command .\script.bat");
只需将.\script.bat
替换为脚本的路径即可。
解决方案2:您可以使用环境变量来获取程序中powershell的路径。只需在系统中设置一个名为PSPATH
的环境变量,或者其他什么,使用powershell目录(C:WINDOWSsystem32WindowsPowerShellv1.0
)的路径。然后您可以使用getenv()
函数来获取它。
示例:
char* psVar = getenv("PSPATH");
if (psVar != NULL)
{
std::string psPath = psVar;
std::string psScript = ".\script.bat";
system(psPath + "\powershell.exe -Command " + psScript);
}
相关文章:
- 获取当前正在运行的 exe 名称(不是路径)
- 我应该如何获取配置文件的绝对路径与 Windows .exe一起发布?
- 无法获取进程 ID 4 (ntoskrnl.exe) 的可执行路径
- GNuPlot.exe找不到用于用 C++ 编写的路径
- 获取 exe 文件夹路径的最佳方法?
- C++ 以编程方式检索 Java.exe 路径
- 尝试使用窗口注册表中的路径和 DeleteFile() 方法删除.exe文件
- 使用 C++ 获取默认的 Windows 文本编辑器.exe路径
- 以编程方式获取powershell.exe的完整路径
- 如何使用Qt Windows中的设置路径属性从命令行运行应用程序(.exe)时在命令行上打印日志
- Qt 创建者编译错误"找不到 UIC.exe从 msvc 2012 路径"
- 有了通往.exe的路径,我如何才能知道它是否正在运行
- 设置 cl.exe 的输出路径
- 如何找到 exe 的路径
- C++ 执行路径中带有空格的.exe
- 如何强制 cmake 在没有完整路径的情况下使用 cl.exe
- 代码块编译的exe链接到一个没有系统路径变量(C++)的dll
- visualstudio2013在运行.exe时修改路径变量
- 无法设置msbuild.exe Visual Studio 2010的路径
- c++将dll放置在exe的相对路径中