Launch a console application from the Form window

I have a Windows console application (which takes parameters) and starts the process. I was wondering if there is any way to start this application from an event window with a window shape button. I would also like to pass an argument to him.

thanks

+3
source share
3 answers

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

+6
source

Assuming you have a form with a multi-line text box 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;
        }
    }
+3
source

System.Diagnostics.Process

+1

Source: https://habr.com/ru/post/1715133/


All Articles