【问题标题】:C# Run command on command prompt and show the commandC# 在命令提示符下运行命令并显示命令
【发布时间】: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。
  • 不是输入输出。子进程的输出被重定向回调用进程,但你没有做任何事情。您无需重定向两个流即可使用输入流。

标签: c# cmd command


【解决方案1】:

为了更清楚,这里是我的评论的附录:

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 控制台中。

【讨论】:

    【解决方案2】:

    你为什么要使用 streamwriter ? 你可以使用参数

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-01
      • 2010-11-30
      • 2021-08-28
      • 2019-05-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多