1

If I go to Start > Run and type in

C:\folder\Program.exe A=1

My program works.

It also works if I create a shortcut to the .exe and edit the Properties to append A=1 to the Target path.

However if I try to run

var p = new Process();
p.StartInfo.FileName = @"C:\folder\Program.exe";
p.StartInfo.Arguments = "A=1"
p.Start();

or

Process.Start(@"C:\folder\Program.exe, "A=1");

it doesn't.

What is the difference between these two calls, and how can I change my code call so it runs exactly the same way as when I type the value into Start > Run?

Rachel
  • 130,264
  • 66
  • 304
  • 490
  • How do you know it does not work? – Edin May 22 '14 at 21:21
  • @Edin The command line argument is required to run the program. When it is missing, the program's shell loads, then an exception occurs and the program doesn't finish loading. – Rachel May 22 '14 at 21:22
  • What is the exception? – BradleyDotNET May 22 '14 at 21:22
  • What is the exception? – Chris Hinton May 22 '14 at 21:22
  • The exception is not relevant here. It is a problem with loading the program, and I can reproduce the exception by running the program without the command line argument. – Rachel May 22 '14 at 21:23
  • 1
    Does `Program.exe` have any dependencies? – Chris Hinton May 22 '14 at 21:24
  • 3
    @Rachel Does your child process have any dependency on current working directory? I believe when you do Start->Run, the shell actually sets current working directory to that where child exe resides. But not with `Process.Start`. Try explicitly specifying current working directory for the child process, and see if it works. I've stepped on this rake more than a few times... – LB2 May 22 '14 at 21:26
  • Are you using any environment variables? – MeTitus May 22 '14 at 21:28
  • Same problem answered here: http://stackoverflow.com/questions/114928/net-process-start-default-directory – ashishduh May 22 '14 at 21:28
  • @LB2 Yes that is it! Thank you very much for explaining that, and if you post that as an answer I'd accept it. I did not think the working directory was relevant since I was using absolute path names :) – Rachel May 22 '14 at 21:30
  • I can just say, that your process starting is fine (apart from the probable typo, i.e. missing " after Program.exe). Give it a try with WorkingDirectory, and if that does not work, than build in some loggging into your Program.exe to see if it is invoked at all and if yes how far you get in the flow. – Edin May 22 '14 at 21:32

1 Answers1

5

Re-posting comment as answer (and rephrasing):

Unlike explorer shell's Start->Run, which automatically sets current working directory to the same folder where target executable is located, .NET's Process.Start does not do that. This is often the case where executable tries to load resources relative to current working folder and expecting it to be same one where executable is located (such as loading config files, DLLs and libraries, etc.) that is leading to crashes or other failures.

Use StartInfo.WorkingDirectory to specify working folder to where the executable is located, and 9 out of 10 times, that'll be it!

Yeap, I've stepped on this rake more than once...

LB2
  • 4,802
  • 19
  • 35