I have a section in my code where I call a batch file and give it certain arguments. This batch file calls another batch file and so on. The entire process takes about 45 minutes to complete. I also need to wait for the batch file to finish before I continue with the rest of my code (cleaning up after the batch file etc).
My problem is that, although I have tried several different things, I cannot get the batch file to both complete its run as well as write its output to a log file.
Here is what I've done to get the batch file to complete its run:
Process process = new Process();
process.StartInfo.WorkingDirectory = Path.GetDirectoryName(filename);
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = false;
process.StartInfo.FileName = filename;
process.Start();
process.WaitForExit();
In an attempt to enable logging I have tried quite a few things. Here is just the latest attempt.
Process process = new Process();
process.StartInfo.WorkingDirectory = Path.GetDirectoryName(filename);
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.FileName = filename;
process.Start();
StreamReader sr = process.StandardOutput;
StreamWriter sw = new StreamWriter(exelocation + @"Logs\" + version + "\\" + outputname + ".txt");
while (!process.HasExited)
{
sw.Write(sr.ReadToEnd());
}
This code basically makes it to a trivial part in one of the first batch files and stops there. The batch file runs for under a minute. I don't understand why this is happening. It runs perfectly if the standard output is not redirected and a window is created but does nothing if the window is hidden and the standard output redirected?