Multiple CMD commands C#

给你一囗甜甜゛ 提交于 2020-05-16 07:36:07

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!