I am trying to call a batch file from an application, but I want the command window to be hidden and the standard output to be redirected to one or more locations (as it is produced by the batch file).
My issue is that when the batch file is running the console is up and nothing displays; it’s just up. When the task completes, the console closes. I want to get rid of the console (perhaps have it run in the back ground).
The other issue is that I am redirecting the output to a richtext box. If i redirect it to the console or text box, it just spits out all the results at once. I would like it to spit out line by line as it happens. Make sense?
The code is below:
//Declare and instantiate a new process component.
System.Diagnostics.Process process1;
process1 = new System.Diagnostics.Process();
process1.StartInfo.UseShellExecute = false;
process1.StartInfo.RedirectStandardOutput = true;
process1.StartInfo.FileName = "cmd.exe";
process1.StartInfo.Arguments = "<BATCHfILE>";
process1.Start();
string output = process1.StandardOutput.ReadToEnd();
rchsdtOut.Text = output;
Console.WriteLine(process1.StandardOutput.ReadToEnd());
process1.WaitForExit();
process1.Close();
This is how I would have done it. Hope I understood your question correctly:
Add:
And then a method
To solve the Cross-thread operation issue mentioned in the comments. You will need to add this to your form class (before the functions begin):
Then you need to add this:
And then in the myMethod function add: