Maurício Linhares
Maurício Linhares

Reputation: 40313

Calling ProcessStartInfo with a path with spaces always breaks

I'm calling a command line program with the following configuration:

var processStartInfo = new ProcessStartInfo {
    FileName = mainCommand,
    UseShellExecute = false,
    RedirectStandardOutput = true,
    RedirectStandardError = true,
    CreateNoWindow = true,
};

And when I'm running it with mainCommand being a path that has no spaces it always works, if there is a space on the path to the command it fails with:

Could not find the command file at C:\Users\Some

Where the actual path would be:

C:\Users\Some User\AppData\Local\Temp\Process.exe

So, why isn't it being escaped and is there a way I can escape this path name to prevent this error?

Upvotes: 1

Views: 3968

Answers (1)

Francis Ducharme
Francis Ducharme

Reputation: 4987

Try wrapping it with quotes:

string targetExe = "\"C:\\this path with spaces\\helloWorld.exe\"";

It works like such, but it also work without having to worry about it as Patrick Hofman said. Something's different on your system it seems.

If you want to pass arguments, do it trough Arguments in ProcessStartInfo. Obviously, if these have spaces too (ie: /arg1 "An Argument"), you will have to wrap them in quotes as above.

Upvotes: 3

Related Questions