Get application path of another executable within

2019-05-02 13:56发布

问题:

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

回答1:

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



回答2:

If you set the Project2 output directory to a known place for both Debug and Release configurations, you can uses that known place in your Project1.