问题
I am using ProcessStartInfo class to execute a batch file. But I am not so aware of using it properly. When I execute a batch file can we track is there any error or not. In my scenario, batch will start another process and it is successfully started it will be keep on executing. And if there is any error executing batch file it will exit. This is the code I am using.
I have to track both successfull start and failure as well.
internal bool ExecuteBatchFile(string fileName, int executionTime)
{
var exitCode = -1;
var error = string.Empty;
try
{
var hubStartInfo = new ProcessStartInfo
{
CreateNoWindow = false,
FileName = fileName,
UseShellExecute = false
};
var process = new Process { StartInfo = hubStartInfo };
process.Start();
process.WaitForExit(executionTime);
if (process.HasExited)
{
exitCode = process.ExitCode;
if (exitCode == 0)
{
return true;
}
error = process.StandardError.ReadToEnd();
}
else
{
return true;
}
return false;
}
catch (Exception e)
{
return false;
}
}
Thanks
回答1:
See here for info on launching a process. Example 2 has info on monitoring the external process
private void simpleRun_Click(object sender, System.EventArgs e)
{
System.Diagnostics.Process.Start(@"C:\listfiles.bat");
}
来源:https://stackoverflow.com/questions/11454644/executing-batch-file-with-processstartinfo