views:

94

answers:

4

I have a .net Console Application called FooConsole. When I build and deploy it, I see that the App.config file from my project is deployed to FooConsole.exe.config.

How can I deploy the .config file to Foo.config instead of FooConsole.exe.config and still have it read as the default .config file?

UPDATE: I realize this sounds like an arbitrary requirement. I wanted to be able to write scripts that would copy custom .config files for different deployments. However, if my .config file name must depend on the name of the .exe, then if someone (likely) decides to change the name of the .exe file, it will break my scripts.

From your answers, it sounds like this is an issue I will simply have to work around.

+3  A: 

The better question is: why do you want to? What's wrong with the default that bothers you?

That said, you can programmatically load a configuration file if you like, and you can name it whatever you like. This answer to a similar question links to all you need to know.

Randolpho
+2  A: 

You cannot. The .NET configuration system will always try to find (exe file name).config - for your foo.exe, .NET will always look for foo.exe.config. There's no switch or setting or something to change that.

If you really really want to deviate from this convention (really?), then you'll need to roll your own using the Configuration class from the System.Configuration namespace.

If you really must change the config name (again: really? And why?), then use this code:

 ExeConfigurationFileMap map = new ExeConfigurationFileMap();
 map.ExeConfigFilename = "Custom.config";  // give it the name you want

 Configuration custom = 
     ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);

 string value = custom.AppSettings.Settings["key"].Value;

You need to add "custom.config" to your project, and make sure it gets "copied to output directory if newer".

This works - you can basically load any file you want - but it's a bit of a kludge nonetheless...

marc_s
A: 

Actually assuming you wantt to have a central configuration - the solution is simple: DONT USEE THE DEFAULT CONFIGURATION FILE FOR THAT... ...but roll your own mechanism. Reading a XML file is not that hard.

TomTom
That doesn't sound right! Why wouldn't you want to use a built in feature available to you and very easy to use and convenient at the same time?
tzup
because you may have 20 .exe files all in the same folder that all go to the same settings. Maintaining them in 20 different config files is hardly "very easy and convenient". loading a "known config file" is a one liner in every application and can be strong typed. Not every application is a small website, tzup. Some are a little or a lot more complicated.
TomTom
+1, doesn't deserve a downvote... if its a central configuration file then yes, going for a "roll your own" is one possible solution
Robin Day
+3  A: 

Whilst you can't change what the default config file is, you can tell this config file to pull its setting from another file. e.g.

<configuration>
    <appSettings file="MyOther.config"></appSettings> 
</configuration>

http://msdn.microsoft.com/en-us/library/ms228154.aspx

Robin Day