Friday, August 7, 2009

Starting another .NET program using the Application Domain Class

The AppDomain class allows you to start another .NET assembly in it's own memory space (separate from your main program's memory space). This is different from executing a process from a thread which is contained in your main program's memory space.
Here's an example of how you can do this with the .NET assembly's path:
AppDomain d = AppDomain.CreateDomain("New Domain");
d.ExecuteAssembly(@"C:\LoginToExternalApp.exe");
You can't call just any exe though. It has to run within the .NET Framework runtime. So something like this won't work:
AppDomain d = AppDomain.CreateDomain("New Domain");
d.ExecuteAssembly(@"C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.exe");
Another way to start an app is to create a reference to it and then call ExecuteAssemblyByName.
AppDomain appDomain = AppDomain.CreateDomain("New Domain");
appDomain.ExecuteAssemblyByName("LoginToExternalApp");
The user may manually close the application you start up programmatically. If you need to close it down programmatically though, you can use AppDomain.Unload method:
AppDomain.Unload(d);


More Info: MSDN: AppDomain Class