【发布时间】:2014-09-02 20:33:53
【问题描述】:
我正在尝试创建一个启动 cmd.exe 并发送命令的应用程序。该命令在 cmd 上可见是很重要的。这是我到目前为止,但它似乎没有工作。有什么想法吗?
Process myProc = new Process();
myProc.StartInfo.FileName = "cmd.exe";
myProc.StartInfo.RedirectStandardInput = true;
myProc.StartInfo.RedirectStandardOutput = true;
myProc.StartInfo.UseShellExecute = false;
myProc.Start();
StreamWriter sendCommand = myProc.StandardInput;
sendCommand.WriteLine("run.exe --forever"); //I want this command to show up in cmd
执行上面的代码时,run.exe 会运行,但命令不会显示在 cmd 中。 我做错了什么?
【问题讨论】:
-
您可能没有看到它,因为您重定向了标准输出。尝试将其注释掉,看看会发生什么。
-
无法在没有重定向的情况下运行 StandardInput。
-
不是输入输出。子进程的输出被重定向回调用进程,但你没有做任何事情。您无需重定向两个流即可使用输入流。