12

I am currently trying to get an executable to be launched from a specific folder.

The code I have below crashes the application oddly enough:

Process p = new Process();
p.StartInfo.WorkingDirectory = "dump";
p.StartInfo.FileName = s;
p.Start();

I debugged it, and it was saying it can't find the file to start, but the file / folder defintly exists, is my syntax bad?

The code below works, but a working directroy is not defined, so it can't find the executable

Process.Start(@"dump\", s);
VMAtm
  • 27,943
  • 17
  • 79
  • 125
James
  • 1,459
  • 3
  • 16
  • 15

2 Answers2

23

The working directory that you set ("dump") is relative to the current working directory. You might want to check the current working directory.

You should be able to set the working directory to the executing assemblies directory with this code...

string exeDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
Directory.SetCurrentDirectory(exeDir);

Or, better yet, don't use a relative path, set p.StartInfo.WorkingDirectory to the absolute path.

Brian
  • 37,399
  • 24
  • 94
  • 109
  • Correct, I have a dump folder in my current orking directroy – James Jul 16 '11 at 18:25
  • Are you missing a using? I cannot find the method `Directory.SetCurrentDirectory(path)` ... did you mean `System.IO.Directory.SetCurrentDirectory(path)` ? – Matt Mar 09 '22 at 09:21
0

You can't start an executable from a folder, if it is not there. You must copy your executable to this folder, and only after that you can start a Process for this executable.

VMAtm
  • 27,943
  • 17
  • 79
  • 125
  • 1
    The working directory is independent from the location of the executable that is started, though. This is an important distinction here. – Nyerguds Dec 19 '19 at 12:54