Changing the .config file associated with an application
That previous post made me think of another problem I had to solve a while back...
Unfortunately, you can't change the .config file your AppDomain is using; it's a read only properly. However, there have been times I would like to have a different config file, perhaps one specified on the command line. Microsoft gives you all the wonderful configuration file functionality, which can be quite useful. Sometimes you have multiple configuration files and want to switch which configuration information you're using.
Here's a quick snippet of code that looks for the name of the configuration file to use on the command line and then lanuches an application with an AppDomain containing the correct configuration file. The configuration file must be in the same directory as the executable you're lanuching.
static void Main(string[] args)
{
if ( args.Length != 1 )
{
Console.WriteLine( "Usage: launcher [config file name]" );
Console.WriteLine( "The config file MUST be in the directory you run launcher from!" );
}
else
{
Assembly a = Assembly.GetExecutingAssembly();
string dir = Path.GetDirectoryName( a.Location );
AppDomainSetup info = new AppDomainSetup();
string myAppPath = String.Format( @"{0}\MyApp.exe", dir );
info.ApplicationBase = @"file:///" + dir;
info.ApplicationName = "MyApp.exe";
// It appears that we have to specify the complete path for the configuration file.
// I tried setting info.ApplicationBase to Environment.CurrentDirectory, but then
// it doesn't appear to be able to launch buildIt.exe. This should work for now,
// but maybe we can improve it in the future.
info.ConfigurationFile = String.Format( @"{0}\{1}", Environment.CurrentDirectory, args[0] );
AppDomain domain = AppDomain.CreateDomain( "MyApp.exe", AppDomain.CurrentDomain.Evidence, info );
string[] cmdLine = {"/myOption"};
domain.ExecuteAssembly( myAppPath, AppDomain.CurrentDomain.Evidence, cmdLine );
// Clean up when done.
AppDomain.Unload( domain );
}
Launch an executable from .Net
A simple concept, but plowing through the .net help to find the class I need to launch an .exe from a .net application wasn't too productive.
So, I went to Google and found it quickly. You use the
Process class. Populate
Process.StartInfo with the the necessary information of what you want to launch.
Vacation
Hooray :) I was gone for a few weeks. Now it's back to the grind :(