简体   繁体   English

多个 CMD 命令 C#

[英]Multiple CMD commands C#

Currently I am programming a program which controls the CMD.目前我正在编写一个控制 CMD 的程序。 The problem is that with my code I can run a single command and then the CMD closes.问题是我的代码可以运行一个命令,然后 CMD 关闭。 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.我想要的是让 CMD 保持打开状态。 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?如果您需要返回值/输出,为什么不能只运行命令并将 output 重定向到带有 mycommand.exe >> %temp%\output.txt 的文件?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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