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

4

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

谢谢

3个回答

7

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;
        }
    }

1
你需要使用 System.Diagnostics.Process。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接