【问题标题】:Run a console application from a windows Form从 Windows 窗体运行控制台应用程序
【发布时间】:2010-11-19 12:16:03
【问题描述】:

我有一个 Windows 控制台应用程序(接受参数)并运行一个进程。 我想知道是否有任何方法可以从 Windows 窗体按钮单击事件中运行此应用程序。我也想给它传递一个参数。

谢谢

【问题讨论】:

    标签: c# .net winforms console-application


    【解决方案1】:

    只需将System.Diagnostics.Process.Start 与控制台应用程序的路径一起使用,并将参数作为第二个参数。

    【讨论】:

      【解决方案2】:

      假设您有一个带有名为 txtOutput 的多行文本框的表单.....

      private void RunCommandLine(string commandText)
          {
              try
              {
                  Process proc = new Process();
                  proc.StartInfo.CreateNoWindow = true;
                  proc.StartInfo.UseShellExecute = false;
                  proc.StartInfo.RedirectStandardOutput = true;
                  proc.StartInfo.RedirectStandardError = true;
                  proc.StartInfo.FileName = "cmd.exe";
                  proc.StartInfo.Arguments = "/c " + commandText;
                  txtOutput.Text += "C:\\> " + commandText + "\r\n";
                  proc.Start();
                  txtOutput.Text += proc.StandardOutput.ReadToEnd().Replace("\n", "\r\n");
                  txtOutput.Text += proc.StandardError.ReadToEnd().Replace("\n", "\r\n");
                  proc.WaitForExit();
                  txtOutput.Refresh();
              }
              catch (Exception ex)
              {
                  txtOutput.Text = ex.Message;
              }
          }
      

      【讨论】:

        【解决方案3】:

        您需要使用 System.Diagnostics.Process

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-01-19
          • 1970-01-01
          • 1970-01-01
          • 2011-03-14
          • 1970-01-01
          相关资源
          最近更新 更多