tags:

views:

39

answers:

3

I want to run a program with different configuration file, the program write with C# 2.0, I make some different file name {program_name}.exe.config, I mean one exe with different config file, for example I have 3 config file, then I will run 3 exe with the different config file, bu the exe file is the same one. Can I do not modify the program for read the different config file (I don`t want to put the config file path in the exe command parameters) to do that(like use the batch file or other method.) ?

Thanks.

+1  A: 

The main issue you have with three configs and one executable is that you need to specify to the executable which config to use.

One option is to make 3 copies of your executable, exe1.exe, exe2.exe and exe3.exe and have a similarly named config for each - exe1.exe.config, exe2.exe.config and exe3.exe.config.

When running each executable, it will use the correct config.

Another option is to have several batch files that will rename the different config files according to which one you want to use. Then you have a single exe and three configs.

Oded
A: 

You create a second executable, and always run that one first. In it, all you do is rename one configfile to the correct name and fire the main application.

string currentConfig = "application.exe.config";
string someOtherName = "firstconfig.config";
string configFileYouWant = "secondconfig.config";
string application = "application.exe";

File.Move(currentConfig, someOtherName);
File.Move(configFileYouWant, currentConfig);
Process.Start(application);
Jouke van der Maas
A: 

You can change the configuration file for the application domain in which the exe is loaded. This is done using the SetData method of the AppDomain class. Ensure that this line of code is executed as the first line of your application.

I have used following code to share 1 exe.config file between 3 different executables.

AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE","yourSharedConfig.exe.config");

You can look at the following blog entry
Binding to custom app.config

If you want to run the same exe with 3 different configs, I believe the same approach will work with bit of customization. You can pass the name of the config file while invoking the exe as a command line parameter and using the SetData method you can dynamically set the config.

Nilesh Gule