问题
Currently I am programming a program which controls the CMD. The problem is that with my code I can run a single command and then the CMD closes. But I need to fill in follow-up commands (like filling in passwords).
My current code is:
cmdProcess.StartInfo.FileName = "cmd.exe";
cmdProcess.StartInfo.CreateNoWindow = false;
cmdProcess.StartInfo.RedirectStandardInput = true;
cmdProcess.StartInfo.RedirectStandardOutput = true;
cmdProcess.StartInfo.UseShellExecute = false;
cmdProcess.Start();
cmdProcess.StandardInput.WriteLine(txt_Command.Text);
cmdProcess.StandardInput.Flush();
cmdProcess.StandardInput.Close();
txt_Output.Text = cmdProcess.StandardOutput.ReadToEnd();
I understand that my code explicitly says that it should close. But when I remove that line the command won't be executed. Anyone who knows why?
What I want is for the CMD to stay open. I hope someone can help.
With kind regards
回答1:
Maybe we need to understand why you need to run the commands in the same context. If you need the return values/output, why can't you just run your command and redirect the output to a file with mycommand.exe >> %temp%\output.txt?
来源:https://stackoverflow.com/questions/61318255/multiple-cmd-commands-c-sharp