确定程序是在Powershell还是CMD上运行

Ami*_*mal 2 c++ windows powershell cmd

我创建了一个程序,应该有rename一些文件

system("rename file.txt file2.txt"); // examples only
Run Code Online (Sandbox Code Playgroud)

确实运行良好cmd,但不是powershell

rename : The term 'rename' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ rename
+ ~~~~~~
    + CategoryInfo          : ObjectNotFound: (rename:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
Run Code Online (Sandbox Code Playgroud)

通过MS的这篇文章powershell应该使用rename-item
但是,我不知道任何“技巧”来确定程序是否运行powershell确定程序运行的cmd
帖子,使用我发现很难实现的进程检查(他们说使用,但我不知道如何做,需要进一步研究)wmic.exe


结论:

如何确定我的程序是否运行于powershellcmdby C++
通过了解(编程)使用的控制台,我的程序是否可以使用if-else方法来更改command

编辑:暂时,我的程序的方法被读取file.txt并将其粘贴到file2.txt(仅示例)..基本上就像copy and paste另一个重命名的文件并使用system("del file.txt");

mkl*_*nt0 5

如何通过C++确定我的程序是在powershell还是cmd上运行

虽然这是可能的,但它也与您的用例无关,因为启动程序的 shell 是程序的进程(您无法向其提交命令)。

由于您的程序必须启动自己的shell(子)进程才能执行 shell 命令,因此您可以自由选择要调用哪个 shell。

Csystem()库函数以主机平台的默认shell为目标,该默认 shellcmd.exe在 Windows(以及/bin/sh类 Unix 平台)上,因此您的命令(使用内部cmd.exe rename命令)将正常工作,无论您的程序是从 PowerShell 还是从cmd.exe.