Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get application path of another executable within same solution

I am using c#, VS 2005.

I have one solution with two projects.

Project1 needs to startup project2 after some checks.

How do I get the executable path of Project2 from within Project1?

I need a solution for both debug and live mode.

thanks,

KS

like image 283
Perplexed Avatar asked Oct 15 '22 06:10

Perplexed


1 Answers

The EXE for the 2nd project needs to have a predictable location, relative from the 1st EXE. Getting the absolute path for the folder that contains your first EXE is easy:

        string myPath = System.Reflection.Assembly.GetEntryAssembly().Location;
        string myDir = System.IO.Path.GetDirectoryName(myPath);

Then append the relative path of your 2nd EXE. Keeping it in the same directory as the 1st is strong recommended:

        string path = System.IO.Path.Combine(myDir, "project2.exe");
        System.Diagnostics.Process.Start(path);

The easiest way to get this to work well in the IDE as well as on the target machine is to let the IDE copy project2.exe. Right-click project1, Add Reference, Projects tab, select Project2. The Copy Local property of the reference will be True so that project2.exe ends up in the same directory as project1.exe

like image 56
Hans Passant Avatar answered Oct 19 '22 04:10

Hans Passant