How to change the default .config file name in a .net project

I have a .net console application called FooConsole. When I create and deploy it, I see that the App.config 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 read it as the default .config file?

UPDATE: I understand 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 should depend on the name of .exe, then if someone (most likely) decides to change the name of the .exe file, it will break my scripts.

From your answers, it seems like this is a problem that I just have to work with.

+4
source share
4 answers

While you cannot change the default configuration file, you can tell this configuration file that it pulled its settings from another file. eg.

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

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

+3
source

Best question: why do you want? What happened to the default that bothers you?

However, you can programmatically download the configuration file if you want, and you can name it as you like. This answer to a similar question refers to everything you need to know.

+2
source

You can not. 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 is no switch or setting or anything to change.

If you really want to deviate from this convention (really?), You need to collapse your own using the Configuration class from the System.Configuration namespace.

If you really have to change the configuration name (again: really? And why?), 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 that it is "copied to the output directory if it is newer."

It works - you can basically upload any file you want, but nonetheless it’s a bit of a bug ...

+2
source

In fact, if you want to have a central configuration - the solution is simple: DO NOT ALLOW THE DEFAULT CONFIGURATION FILE FOR THIS ... ... but roll your own mechanism. Reading an XML file is not that difficult.

0
source

Source: https://habr.com/ru/post/1304875/


All Articles