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"

3
  • 1
    @duDE That's not necessarily a duplicate. Commented Apr 15, 2013 at 8: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 Commented Apr 15, 2013 at 8:51
  • The same @Steve no process was done Commented Apr 15, 2013 at 8:53

1 Answer 1

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();
}
Sign up to request clarification or add additional context in comments.

5 Comments

No Process i did not see any result @Arshad
is your batch file working ?
yup it work great if i execute it manually
thank you all it works i tried link suggested by @SatpalSingh and it works well thank you all for your help thank you saptalSingh
@Arshad Why do you catch the exception of objProcess.Start(); and swallow it which may contain valuable information.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.