I have a windows form that executes a batch file. I want to transfer everything that happends in my console to a panel in my form. How can I do this? How can my DOS console
I recently build a tiny app where I was interacting with batch files. I found this snippit of code that allowed me to do this:
Process proc = new Process
{
StartInfo =
{
RedirectStandardError = true,
RedirectStandardOutput = true,
UseShellExecute = false,
}
};
proc.Start();
string errorMessage = proc.StandardError.ReadToEnd();
proc.WaitForExit();
string outputMessage = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
From here, just direct those strings into a usercontrol of your choice.
EDITS
Note: This is not a general solution. It can lead to deadlocks. From the doc for RedirectStandardError:
Synchronous read operations introduce a dependency between the caller reading from the StandardError stream and the child process writing to that stream. These dependencies can cause deadlock conditions. When the caller reads from the redirected stream of a child process, it is dependent on the child. The caller waits for the read operation until the child writes to the stream or closes the stream. When the child process writes enough data to fill its redirected stream, it is dependent on the parent. The child process waits for the next write operation until the parent reads from the full stream or closes the stream. The deadlock condition results when the caller and child process wait for each other to complete an operation, and neither can continue. You can avoid deadlocks by evaluating dependencies between the caller and child process.