akapulko2020
akapulko2020

Reputation: 1139

How do I use ProcessStartInfo to run a batch file?

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?

Upvotes: 9

Views: 18727

Answers (3)

Martin Schneider
Martin Schneider

Reputation: 15378

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();

Upvotes: 1

npocmaka
npocmaka

Reputation: 57242

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

Upvotes: 0

Steven Sudit
Steven Sudit

Reputation: 19620

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

Upvotes: 8

Related Questions