-2

I tried all the solutions to the other questions, but none of them worked. I also tried "call filename.exe>log.txt" in CMD, but it didn't work. I'd appreciate it if you could help me with this.

I am a non-English-speaking student, so the expression may be strange. I'd appreciate your understanding.

using (Process process = new Process())
                {
                    process.StartInfo.FileName = ProcessPath;
                    process.StartInfo.UseShellExecute = false;
                    process.StartInfo.RedirectStandardInput = true;
                    process.StartInfo.RedirectStandardOutput = true;
                    process.StartInfo.RedirectStandardError = true;
                    process.StartInfo.WorkingDirectory = Path.GetDirectoryName(ProcessPath);
                    process.Start();

                    while (process.HasExited)
                    {
                        TextBox1.AppendText(process.StandardOutput.ReadLine()+"\r\n");
                    }

                    process.WaitForExit();
                }
JustTemp
  • 65
  • 1
  • 5
  • [How do I get output from a command to appear in a control on a Form in real-time?](https://stackoverflow.com/a/51682585/7444103) -- Read the notes about the `SynchronizingObject` property. – Jimi Aug 13 '21 at 06:17
  • _Doesn't work_ is not a helpful problem description! – TaW Aug 13 '21 at 06:18
  • 1
    `while (process.HasExited)` is an endless loop. The state can't change to false anymore. Instead of this you can `process.StandardOutput.ReadToEnd();` This is a blocking call, it waits until the process has finished. (see https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.standardoutput?view=net-5.0) – Michael Aug 13 '21 at 06:25
  • I don't excactly understand what output you mean? Do you want to display the text of a generated file called log.txt? – user1673665 Aug 13 '21 at 06:53
  • Does this answer your question? [How to capture the standard output/error of a Process?](https://stackoverflow.com/questions/3633653/how-to-capture-the-standard-output-error-of-a-process) and [C# process hanging due to StandardOutput.ReadToEnd() and StandardError.ReadToEnd()](https://stackoverflow.com/questions/47214380/c-sharp-process-hanging-due-to-standardoutput-readtoend-and-standarderror-read) and [StandardOutput.ReadToEnd() hangs](https://stackoverflow.com/questions/7160187/standardoutput-readtoend-hangs) –  Aug 13 '21 at 07:16

2 Answers2

1

Firstly, you are checking for process.HasExited in a while loop. This, of course will be false by default, and then your code will just skip this. That is why I suggest using an asynchronous method or an event-based aproach.

If you choose asynchronous, you can do this:

using (var process = Process.Start(psi))
{
    errors = await process.StandardError.ReadToEndAsync();
    results = await process.StandardOutput.ReadToEndAsync();
}

here, psi is an instance of ProcessStartInfo.
You set them after creating the process, but you can create an object and pass that in the constructor.

If you cannot make it asynchronous, you can do:

using (var process = Process.Start(psi))
{
    errors = process.StandardError.ReadToEndAsync().Result;
    results = process.StandardOutput.ReadToEndAsync().Result;
}
Berlm
  • 447
  • 3
  • 7
1

Use events and set them before start:

process.ErrorDataReceived += (sendingProcess, errorLine) => error.AppendLine(errorLine.Data);
process.OutputDataReceived += (sendingProcess, dataLine) => SetLog(dataLine.Data);
s.d.fard
  • 181
  • 2
  • 9