简体   繁体   中英

Multiple CMD commands C#

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

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM