Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
多个CMD命令由c++;(或c#)_C#_C++_Cmd - Fatal编程技术网

多个CMD命令由c++;(或c#)

多个CMD命令由c++;(或c#),c#,c++,cmd,C#,C++,Cmd,嘿,我只是想知道这是否适用于运行多个CMD命令?我还没有测试过这个 //multiple commands System::Diagnostics::Process ^process = gcnew System::Diagnostics::Process(); System::Diagnostics::ProcessStartInfo ^startInfo = gcnew System::Diagnostics::ProcessStartInfo(); //startInfo->Wind

嘿,我只是想知道这是否适用于运行多个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
文件并执行它。

您编写的代码执行它看起来的功能。它首先将
参数设置为一个值,然后用另一个值覆盖该值。因此,
Start()
只执行第二个命令

我建议创建一个助手函数(或方法):


根据您要执行的操作,您可能希望在启动后调用
process->WaitForExit()

这不起作用。此代码:

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");

当然,您需要在此基础上添加错误处理等,特别是在当前进程没有正确权限的情况下。

Ah好的,这更有意义,对于这种情况下的错误处理,您的意思是要求用户允许这些命令运行吗?(管理员权限?)。您可以在此处了解有关以管理员权限运行的更多信息:如果这回答了您的问题,请不要忘记接受它!=)不相关,但我总是想知道,当使用IntelliSense时,人们如何能够在多行代码中拖拽变量名中的拼写错误,而不注意…@Joey,非常容易。如果英语不是你的母语也没用。斯维克:啊,我忘了智能感知;现在我的大部分代码都是在文本编辑器中输入的。
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");