C#在命令提示符下运行命令并显示命令

时间:2014-09-02 20:33:54

标签: c# cmd command

我正在尝试创建一个启动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中。 我做错了什么?

2 个答案:

答案 0 :(得分:0)

以下是我的评论的附录,以便更清楚:

Process myProc = new Process();
myProc.StartInfo.FileName = "cmd.exe";
myProc.StartInfo.RedirectStandardInput = true;
//myProc.StartInfo.RedirectStandardOutput = true;
myProc.StartInfo.UseShellExecute = false;
myProc.Start();
System.IO.StreamWriter sendCommand = myProc.StandardInput;
sendCommand.WriteLine("run.exe --forever");

这将允许cmd输出的所有内容显示在cmd控制台中。

答案 1 :(得分:0)

为什么要使用编写器? 你可以使用Arguments

 myProc.StartInfo.Arguments="run.exe --forever";