Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a console application from a windows Form

I have a windows console app (that accepts parameters) and runs a process. I was wondering if there was any way to run this app from within a windows form button click event. I would like to pass an argument to it as well.

Thanks

like image 750
zSynopsis Avatar asked Dec 17 '22 06:12

zSynopsis


2 Answers

Just use System.Diagnostics.Process.Start with the path to the console application, and the parameters as the second argument.

like image 157
Reed Copsey Avatar answered Dec 19 '22 20:12

Reed Copsey


Assuming you have a form with a multiline textbox called 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;
        }
    }
like image 25
Brian Fenske Avatar answered Dec 19 '22 19:12

Brian Fenske