MichaelDev
Monday, August 30, 2004
  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.
 
Monday, August 23, 2004
  Vacation
Hooray :) I was gone for a few weeks. Now it's back to the grind :(
 
Various notes on software development. Currently using C# and .Net. May also include comments on the overall software development process, etc.

Name:
Location: Orange County, California, United States
ARCHIVES
June 2004 / July 2004 / August 2004 / September 2004 / October 2004 / November 2004 / December 2004 / January 2005 / February 2005 / March 2005 / April 2005 / May 2005 / September 2005 / October 2005 / November 2005 / January 2006 / February 2006 / April 2006 / May 2006 / June 2006 / March 2007 /


Powered by Blogger