.NET Config Files configSource outside the application directory folder

Viewed 76715

I have two applications one a console application and the other an ASP.NET app. They both need to know the same appSettings and connectionStrings. So ideally I would like to use the configSource property of app.config/web.config files to point that to a central location. For example

<connectionStrings configSource="D:\connectionStrings.config"/>
<appSettings configSource="D:\appSettings.config"/>

That however fails with an error:

The configSource attribute is invalid.: The configSource 'D:\appSettings.config' is invalid. It must refer to a file in the same directory or in a subdirectory as the configuration file.

Is there anyway to still use the configuration managers appSettings/connectionStrings and get the values from an external location?
I'm happy with having to add code to do it, but I don't want to have to replace the entire configuration manager system.

11 Answers

Another solution is simply to add the configuration file in all your projects as a link instead of actually copying the file to your projects. Then set the "Build Action" of the file to "Content" and "Copy to Output Directory" to "Copy if newer" and when you compile the project you will have the file in the output directory.

To add the file as a link in the "Add Existing Item" dialog box, there is an Add button with a dropdown. Choose "Add as link" from the dropdown on the Add button to complete the process.

It seems that's that is the way it is. configSource must be in the same folder or deeper.

You could, although I'm not sure you should, use an NTFS hardlink. [mad grin]

You can load configuration from an arbitrary location, but it won't be available via the static properties of ConfigurationManager:

Configuration myConfig = ConfigurationManager.OpenExeConfiguration(path)

(There is an overload that allows multuple files to be specified, to support default/user-roaming/user-local hierarchy.)

Losing the static properties means all the code needs to be aware of the different configuration.

You can place both settings in the machine.config and then they are available for all you applications on the server.

The solution I found worked best was to put the "shared" config files in a central files and then use a pre-build event in Visual Studio to copy them to a relative folder of each project which needed it.

You can use the file attribute rather than configSource

There's a good article here on it

This allows you to specify a relative path like so

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings file="..\..\..\..\..\..\ExternalFile.config"></appSettings>
</configuration>

The path is relative to the output directory.

Then in ExternalFile.config you just add the appSettings section

<appSettings>
    <add key="SomeKey" value="alue"/>
</appSettings>

If you have multiple projects that need to share same connection string, put the connection string config file you want to share between different projects in the class library project. And then from the project that needs to use the connection string, do this:

  1. If it's a Console project or Unit Test project:

In Console or Unit Test projects, do this in app.config: <connectionStrings configSource="connectionString.config"/>

That's it.

Why there is no link needed at all? Answer: when you build the project, connectionString.config gets copied to the output folder. The other projects' app.config files also get copied to the output folder. So when you run the start up project, the app.config of the start up project IS in the same folder as connectionString.config. That's why there's no link needed.

  1. If it's a Web project:

In web config do this: <connectionStrings configSource="bin\connectionString.config"/>

Note that if you set connectionString.config file to Copy If Newer, sometimes Visual Studio 2019 doesn't detect the change. So you can just set it to Copy Always.

Related