从Windows窗体运行控制台应用程序

时间:2009-08-14 00:23:06

标签: c# .net winforms console-application

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

由于

3 个答案:

答案 0 :(得分:7)

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

答案 1 :(得分:4)

假设您的表单中包含一个名为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;
        }
    }

答案 2 :(得分:1)

您需要使用System.Diagnostics.Process