Sending input/getting output from a console application (C#/WinForms)

前端 未结 4 487
你的背包
你的背包 2020-12-06 03:48

I have a form with 3 controls:

  1. A textbox for the user to enter commands to send to a console application,
  2. A button to confirm the commands to be sent
4条回答
  •  臣服心动
    2020-12-06 04:28

    I've used code something like this:

        public static void Run(string fileName, string arguments, out string standardOutput, out string standardError, out int exitCode)
        {
            Process fileProcess = new Process();
            fileProcess.StartInfo = new ProcessStartInfo
            {
                FileName = fileName,
                Arguments = arguments,
                RedirectStandardError = true,
                RedirectStandardOutput = true,
                UseShellExecute = false,
                WindowStyle = ProcessWindowStyle.Hidden,
                CreateNoWindow = true,
            };
    
            bool started = fileProcess.Start();
    
            if (started)
            {
                fileProcess.WaitForExit();
            }
            else
            {
                throw new Exception("Couldn't start");
            }
    
            standardOutput = fileProcess.StandardOutput.ReadToEnd();
            standardError = fileProcess.StandardError.ReadToEnd();
            exitCode = fileProcess.ExitCode;
    
        }
    

    But it's not interactive. But if the app is interactive, it'll take a lot more code anyway.

提交回复
热议问题