多个CMD命令管理c++(或c#)
Multiple CMD commands Managed c++ (or c#)
嘿,我只是想知道这是否适用于运行多个CMD命令?我还没有测试过。
//multiple commands
System::Diagnostics::Process ^process = gcnew System::Diagnostics::Process();
System::Diagnostics::ProcessStartInfo ^startInfo = gcnew System::Diagnostics::ProcessStartInfo();
//startInfo->WindowStyle = System::Diagnostics::ProcessWindowStyle::Hidden;
startInfo->FileName = "cmd.exe";
startInfo->Arguments = "/C powercfg -attributes SUB_PROCESSOR 12a0ab44-fe28-4fa9-b3bd-4b64f44960a6 -ATTRIB_HIDE";
startInfo->Arguments = "/C powercfg -attributes SUB_PROCESSOR 40fbefc7-2e9d-4d25-a185-0cfd8574bac6 -ATTRIB_HIDE";
process->StartInfo = startInfo;
process->Start();
或者startInfo
一次只能处理一个参数?如果是这样,我如何执行多个命令而不创建.bat
文件并执行该文件。
这行不通。这段代码:
stratInfo->Arguments = "/C powercfg -attributes SUB_PROCESSOR 12a0ab44-fe28-4fa9-b3bd-4b64f44960a6 -ATTRIB_HIDE";
stratInfo->Arguments = "/C powercfg -attributes SUB_PROCESSOR 40fbefc7-2e9d-4d25-a185-0cfd8574bac6 -ATTRIB_HIDE";
不设置两个参数。它设置参数字符串,然后覆盖它。
如果你想运行两次,你必须这样做:
void RunProc(System::String ^arguments)
{
System::Diagnostics::Process ^process = gcnew System::Diagnostics::Process();
System::Diagnostics::ProcessStartInfo ^startInfo = gcnew System::Diagnostics::ProcessStartInfo();
startInfo->FileName = "cmd.exe";
startInfo->Arguments = arguments;
process->StartInfo = startInfo;
process->Start();
}
RunProc("/C powercfg -attributes SUB_PROCESSOR 12a0ab44-fe28-4fa9-b3bd-4b64f44960a6 -ATTRIB_HIDE");
RunProc("/C powercfg -attributes SUB_PROCESSOR 40fbefc7-2e9d-4d25-a185-0cfd8574bac6 -ATTRIB_HIDE");
当然,你会想要添加错误处理等,特别是在当前进程没有正确权限的情况下。
您编写的代码完成了它看起来要做的事情。它首先将Arguments
设置为一个值,然后用另一个值覆盖它。因此,Start()
只执行第二个命令。
void RunPowerCfg(System::String ^id)
{
System::Diagnostics::Process ^process = gcnew System::Diagnostics::Process();
System::Diagnostics::ProcessStartInfo ^startInfo =
gcnew System::Diagnostics::ProcessStartInfo();
startInfo->FileName = "cmd.exe";
startInfo->Arguments = System::String::Format(
"/C powercfg -attributes SUB_PROCESSOR {0} -ATTRIB_HIDE", id);
process->StartInfo = startInfo;
process->Start();
}
void main()
{
RunPowerCfg("12a0ab44-fe28-4fa9-b3bd-4b64f44960a6");
RunPowerCfg("40fbefc7-2e9d-4d25-a185-0cfd8574bac6");
}
根据您想要做的事情,您可能希望在启动process->WaitForExit()
后调用它。
相关文章:
- 如何使用ndk-build.cmd构建Android.so文件
- 在由Sublime文本3编译后在cmd上显示Java程序输出
- 如何在使用Makefile构建后在cmd中运行该程序?
- 为什么下面的Hello World程序在PowerShell上没有显示任何输出?同一程序在CMD上显示正确的输出
- 父进程意外被子 cmd 杀死.exe运行 java jar 的进程 - WINDOWS 7 问题 - C++
- 使用 CreateProcessWithTokenW 在 CMD 中启动 CMD
- 使用 system() 函数在C++程序中运行 cmd 命令
- cmd.exe与Powershell中C++程序的不同执行时间
- C++ - 如何将空格键转换为短划线以进行cmd输入
- 从Visual Studio 2017运行MPI应用程序,每个进程在不同的cmd窗口中
- 代码编译但不起作用!cmd窗口只是理想和理想,但什么也没发生
- 如何查看完整的CMD执行命令,包括参数和标志?
- 无法使用QProcess在cmd.exe中执行复制命令
- 如何将我的程序转换为.dll文件并使用rundll32.exe在cmd中运行它?
- 仅使用 C++ 和 cmd 创建.lnk文件
- 从 CMD 运行.exe并自动向其传递参数
- 使用Qt在CMD中复制多个文件
- CPP 程序二查找两个给定日期之间的天数,conol CMD
- 如何在Powershell中运行C++程序,就像CMD一样?
- CMD:程序.exe 1< "A:/input.txt"产生无输出