Tomas
Tomas

Reputation: 18127

Load different app.config for .NET console application

I am running .NET console application from shared network folder on several Windows 2008 machines and would like to have separated app.config file for every Windows 2008 machine on which console is run. What is the best method to do that?

Upvotes: 1

Views: 1332

Answers (1)

Christian.K
Christian.K

Reputation: 49290

You have some options, which depend upon whether you can change and recompile the console application or not:

  1. Put copies of your executable and dependent libraries in separate subdirectories and provide custom app.config for each. Like a share/subdirectory per client machine. This could, of course, turn into a maintenance nightmare or space hogging experience. For simple solutions/needs it might still be feasible though.
  2. As a variation of (1) put copies of the same console application binary in the same directory, with different names. Put individual versions of the app.config, named like the copies of the executable in the same directory. Again, this has the same, if not more, issues than (1), but could work, depending on your precise requirements.
  3. Restart your executable itself from a new AppDomain. This allows you to specify a custom path/filename for the app.config for the new application domain. Shameless plug: https://stackoverflow.com/a/3633158/21567. Note that this approach is not limited to executing your own assembly inside the new application domain. You could also write a managed stub application that just does an ExecuteAssembly on the console application you really want to run (which may not be able to change), providing it a custom app.config.
  4. Write a native stub process that hosts the CLR itself. This will also allow you to specifiy the path/name of the app.config file to use.

Upvotes: 1

Related Questions