-1

I have a console application which is run through Process. Inside this console app, I'm running another exe also through a process.

When I click double-click Run.exe in the bin folder, the application works fine. But when I run it through code, it throws an unhandled exception.

enter image description here

Code that starts the console app:

ProcessStartInfo info = new ProcessStartInfo();
info.FileName = @"C:\_Core\Server\bin\Debug\ServerManager.exe";
Process process = new Process();
Process.Start(info);

Code inside the console app which runs another exe:

ProcessStartInfo info = new ProcessStartInfo();
info.FileName = @"C:\_Core\Client\bin\Debug\Run.exe";
Process process = new Process();
Process.Start(info);
jmc
  • 1,649
  • 6
  • 26
  • 47
  • Show the code. It looks like the `Form_Load` handler reads a file that is not there, as your startup directory differs. – CodeCaster Oct 06 '15 at 10:06
  • @CodeCaster I'm just running the `exe`. I wonder how it differs from just double-clicking. – jmc Oct 06 '15 at 10:08
  • I explained the difference, it's the startup directory. – CodeCaster Oct 06 '15 at 10:08
  • @CodeCaster I'm confused, can you expand please? thanks – jmc Oct 06 '15 at 10:11
  • No. Show the code that throws, then you can get an answer that doesn't require workarounds. _Are_ you reading a file in the form's `Load` event handler? – CodeCaster Oct 06 '15 at 10:11
  • I added @Ksv3n answer to my code and it worked. @CodeCaster you are right about the startup directory. What I was confused about is why it was not able to find the `exe` even though I specified the full path. I found an answer here: http://stackoverflow.com/questions/114928/net-process-start-default-directory. Technically, it will search for the `exe` that you are trying to run on `%SYSTEMROOT%\system32`, so specifying where to find it via the `WorkingDirectory` property solves the issue. – jmc Oct 06 '15 at 10:37
  • Yeah if you `Process.Start()` an executable, that executable's working directory is not the directory it resides in. So I guess you use something like `File.Open("foofile.txt")`. You need to fix _that_ code, not the code that calls `Process.Start()` . See http://stackoverflow.com/questions/1658518/getting-the-absolute-path-of-the-executable-using-c and so on. – CodeCaster Oct 06 '15 at 10:38

1 Answers1

2

It may be a wrong value of startup directory. Try this :

info.FileName = @"C:\_Core\Client\bin\Debug\Run.exe";
info.WorkingDirectory = Path.GetDirectoryName(info.Filename);
Process.Start(info);

But the best way is definitely to fix it from the called application on your Form.Load, example

// On the main form
private void Load() 
{
// Before doing anything, fix your current directory :
    string exeDir =  Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
    Directory.SetCurrentDirectory(exeDir);

   ..........
}
Perfect28
  • 11,089
  • 3
  • 25
  • 45
  • does it matter if the `exe` im running is from a different directory? currently I'm hard-coding the `exe` paths – jmc Oct 06 '15 at 10:11
  • yes it does matter. Check the edit I made if you want it to run from a different directory. – Perfect28 Oct 06 '15 at 10:13
  • 1
    You should not fix this from the caller. Get the executable's location in the callee, and use relative paths from there. – CodeCaster Oct 06 '15 at 10:17