Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoking a second Visual Studio project programmatically

I'm working on a solution in Visual Studio 2010 Ultimate that contains two C# projects: a Forms project and a console project. Programmatically, they are completely separate; the main portion of both projects could run without the other. What I would like to do is invoke the console application by pressing a button in the Forms application, with certain options set based on the Forms application. So you can think of the Forms application as essentially just a 'launcher' app.

The way I have this implemented right now is by using System.Diagnostics.Process.Start with "cmd" as the first parameter and the path to the console application's executable (plus CLI arguments) as the second parameter. But this is a terrible solution, because it will break when deployed to other machines with an installer.

I've also tried creating a reference to the console project in the Forms object, making Program in the console project public and static, and calling the console project's Main method, but because there's no valid output handle, this eventually causes an exception to be thrown.

I've done much Googling, some searching here on SO, and even checked the 'Similar Questions' on the submission page, and no one seems to know how to do what I want to do. Any pointers that could put me on the right track?

FYI: I know that there's probably ten million better ways to do this, but I have several reasons for wanting and needing it done this way. I know I could just make a separate GUI for the CLI program and copy the relevant code over, or merge everything into one project and make a separate output window, or various other solutions like those. But I need this solution for two reasons: (1) I had a fairly complex CLI program that works well as a CLI program, and now need a user-friendly way to start it, and (2) solving the problem in other ways doesn't answer the question of "Why bother having several executable projects in the same solution in the first place if you can't invoke one from the other?"

like image 632
Mrrvomun Avatar asked Nov 12 '11 20:11

Mrrvomun


1 Answers

The way you're doing it is fine - it just needs a bit of tweaking.

Firstly, when you launch, don't run cmd with your executable as a parameter, run your console application myapp.exe directly as the command to execute in Process.Start.

The next step is to make your application run from wherever it was installed. The easiest approach to this is to simply deploy both executables side-by-side in the same folder. Use Application.StartupPath to find the folder that your Forms application was launched from (and hence where the other app resides). Use Path.Combine(Path.GetDirectory(Application.StartupPath), "myapp.exe") to generate the full pathname of the exe to run.

Another option is to use an installer that can then store the location of the other app in a known place (e.g. a registry key), but that's a magnitude more complexity for little gain (unless you need the installer for other reasons).

like image 86
Jason Williams Avatar answered Oct 17 '22 02:10

Jason Williams