1

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?

Maurício Linhares
  • 39,901
  • 14
  • 121
  • 158
  • Are you talking about the arguments? The exe works fine here. – Patrick Hofman Apr 02 '15 at 14:21
  • I am talking about the program path, doesn't work here, always fails with that error. – Maurício Linhares Apr 02 '15 at 14:27
  • Can you make this reproducable with a default Windows program? Can't get this failing here. – Patrick Hofman Apr 02 '15 at 14:28
  • Where are you delcaring mainCommand? Make sure you prefixed the string with the `@` character. – ilian Apr 02 '15 at 14:33
  • @PatrickHofman all default windows programs live inside the windows32 folder, there are no spaces at their paths. Are you trying with a program that is at a path with spaces? – Maurício Linhares Apr 02 '15 at 14:35
  • @ilian000 can you elaborate on that? The `mainCommand` value is a string read from a config file that contains a path like `C:\Users\Some User\AppData\Local\Temp\Process.exe`. – Maurício Linhares Apr 02 '15 at 14:36
  • @MaurícioLinhares: Try `@"C:\Program Files\Internet Explorer\iexplore.exe"`. Works fine here. – Patrick Hofman Apr 02 '15 at 14:38
  • @PatrickHofman how do I apply this @ to a string that came from a config file? This isn't hardcoded in the code. – Maurício Linhares Apr 02 '15 at 14:39
  • @MaurícioLinhares: Then you don't need to. The `@` is just to escape string literals. – Patrick Hofman Apr 02 '15 at 14:40
  • Pretty sure Francis Ducharme has it correct on their answer. Surround the path with quotes, inside the string you're passing, so you're actually passing the argument `"C:\Some Exe.exe"` instead of `C:\Some Exe.exe`. That's how you have to wrap spaced path strings on the command line. – Frosty840 Apr 02 '15 at 14:45
  • 1
    Did you debug the value of mainCommand to make sure the path is correct when reading from your config file? – ilian Apr 02 '15 at 14:49
  • possible duplicate of [Use Process.Start with parameters AND spaces in path](http://stackoverflow.com/questions/17321289/use-process-start-with-parameters-and-spaces-in-path) – Maurício Linhares Apr 02 '15 at 17:24

1 Answers1

3

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.

Francis Ducharme
  • 4,848
  • 6
  • 43
  • 81