0

Please help me i have a .bat file in specific location i wanna to execute it and see what happening like if clicked on it manually, the problem is the .bat file running as pop up window for just moment and no process done, My code is like

int exitCode;
            ProcessStartInfo processInfo;
            Process process;
            string command = @"D:\programs\PreRef\Run.bat";
            processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
            //processInfo.CreateNoWindow = true;
            processInfo.UseShellExecute = false;
            // *** Redirect the output ***
            processInfo.RedirectStandardError = true;
            processInfo.RedirectStandardOutput = true;

            process = Process.Start(processInfo);
            process.WaitForExit();

            // *** Read the streams ***
            string output = process.StandardOutput.ReadToEnd();
            string error = process.StandardError.ReadToEnd();

            exitCode = process.ExitCode;
            process.Close();

So Please help me to solve this problem.Note this .bat file run another .exe program and the text in the .bat file is like PreRef.exe "D:\programs\PreRef"

Satpal
  • 132,252
  • 13
  • 159
  • 168
MohamedSayed
  • 147
  • 1
  • 4
  • 14
  • 1
    @duDE That's not necessarily a duplicate. – Grant Thomas Apr 15 '13 at 08:48
  • Just for the debugging effort, try to change the `/c` parameter in `/K` and comment out the last lines that redirects the process output (leave only the `Process.Start` line). Now you should be able to see the command window and read the output of your batch file – Steve Apr 15 '13 at 08:51
  • The same @Steve no process was done – MohamedSayed Apr 15 '13 at 08:53

1 Answers1

1

You can try this:

Process objProcess = new Process();
objProcess.StartInfo.UseShellExecute = false;
objProcess.StartInfo.RedirectStandardOutput = true;
objProcess.StartInfo.CreateNoWindow = true;
objProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;   
//file location
objProcess.StartInfo.FileName = string.Format(@"D:\programs\PreRef\Run.bat";");
//any argument 
objProcess.StartInfo.Arguments = string.Format("");
try
{
 objProcess.Start();
}
catch
{
 throw new Exception("Error");
}
StreamReader strmReader = objProcess.StandardOutput;
string strTempRow = string.Empty;
while ((strTempRow = strmReader.ReadLine()) != null)
{
    Console.WriteLine(strTempRow);
}
if (!objProcess.HasExited)
{
   objProcess.Kill();
}
Mohammad Arshad Alam
  • 9,694
  • 6
  • 38
  • 61