views:

299

answers:

4

I have a .NET Windows application that needs to call another .NET executable. I want to distribute this other exe as part of the main project, and was wondering what is the best way of implementing this. We don't want to take the code from this second exe and put it in the main project as we need the exe to effectively remain sealed, as we are also distributing it to third parties.

Options being considered:

  1. Should I add the exe as a file within the project and set Copy to Output as 'Copy always' and then just run it from the main application folder?
  2. Should I add the exe as a reference in the main project? If I do this how would I then call it as an executable with parameters?

I would be grateful for some guidance on the above approaches, and indeed if you have any other ways of achieving my goal.

Thanks!

A: 

You can also make it as Dll Assembly and call it from your application, I meant loading the assembly at run time if it's existed.

Wael Dalloul
You don't need to make it a DLL to use it as a reference.
Jon Skeet
A: 

Adding a reference to that exe won't help, unless you want to use classes from the exe in you main program. I think your first option is the best

Thomas Levesque
+4  A: 

There are two different ways of executing the code in the exe file:

  • As a separate process: use System.Diagnostics.Process and pass in any information as command line arguments
  • In the same process, using normal .NET method calls
  • In the same process in a separate AppDomain

Both of these work fine; in the first version you wouldn't need your main project to refer to the exe at all: just include it in your setup/deployment project, if you have one. If you're using xcopy deployment (i.e. just taking the contents of the output directory) then you could add a reference to the exe just to get it copied.

To use the second form you'd need to add a reference to the exe file in your project. If you're happy to call the code in-process this is likely to be the simplest solution.

The third form could use a reference but doesn't have to. This gives more isolation than the second option but less than the first.

Jon Skeet
Thanks Jon. A very complete and useful answer!
JamesW
@Jon: First statement ("two different ways") conflicts with actual number of ways you've listed.
Konstantin Spirin
+2  A: 

Copy to output is, of course, the simplest solution.

If you don't want to have this exe hanging around together with your app files, put it to resources and when your application needs to run that exe, just save it to temp folder and run from there.

Here's proof-of-concept code:

class Program
{
    static void Main(string[] args)
    {
        var fileName = Path.ChangeExtension(Path.GetTempFileName(), "exe");
        File.WriteAllBytes(fileName, Resources.QueryExpress);
        var queryAnalyzer = Process.Start(fileName);
        queryAnalyzer.WaitForExit();
    }
}

In project properties go to Resources and drag-drop there your exe file. This sample is using application named QueryExpress.exe.

You may also wish to compress your app into ZIP file to make it smaller. This will add one more step to unpack archive before running the process.

Konstantin Spirin
+1 This is the best option if it's important to make it harder for the end-users to directly run (or even know about) the second executable. The options mentioned in Jon's answer do not achieve this.
Ash