8

But it doesn't work -meaning the java code is not executed. Although the batch file runs fine when clicked in Windows explorer or when run in command line ..

Since this works fine when the batch file is a single DOS command, I think this is somehow related to the fact that the Java code needs ~20 minutes to run. I'm using the following code

var si = new ProcessStartInfo();
si.CreateNoWindow = true;
si.FileName = batchFileName;
si.UseShellExecute = false;
Process.Start(si);

What am I doing wrong?

akapulko2020
  • 1,079
  • 2
  • 22
  • 36

3 Answers3

8

Set UseShellExecute to true, so it loads cmd.exe to run the batch file.

Julien Roncaglia
  • 17,397
  • 4
  • 57
  • 75
Steven Sudit
  • 19,391
  • 1
  • 51
  • 53
0

Check this - a batch file wrapper of ProcessStartInfo:

C:\>ProcessStartJS.bat "cmd.exe" -arguments "/c pause" -style Minimized -priority High -newWindow yes -useshellexecute yes

Started: cmd.exe /c pause
PID:6540
npocmaka
  • 55,367
  • 18
  • 148
  • 187
0

As Lucas Jones mentioned in the comments, if you don't want to use ShellExecute, do it like this:

string fullBatPath = @"C:\path with space\file.bat";

var process = new Process()
{
    StartInfo = new ProcessStartInfo
    {
        FileName = "cmd.exe",
        Arguments = $"cmd /C \"{fullBatPath}\"",
        UseShellExecute = false,
        CreateNoWindow = true,
    }
};
process.Start();
Martin Schneider
  • 14,263
  • 7
  • 55
  • 58