2

I have a problem with the app.config file. When the configuration file is next to the exe file, then it works perfectly.
But when I place the external config file to another network drive, then it throws a "configuration system failed to initialize" error.
How should I link to the external config file from the app config to get it to work?

Here is the code:

Program.cs
static void Main(string[] args)
{
   var conn = ConfigurationManager.ConnectionStrings["conn"];
   Console.WriteLine("ConnStr01={0}", conn);
   Console.Read();
}

App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <connectionStrings configSource="U:\config\conStr.config">
  </connectionStrings>
</configuration>

conStr.config
<?xml version="1.0" encoding="utf-8" ?>
<connectionStrings>
  <add name="conn"
    connectionString="Data Source={server};Initial Catalog={database};Persist Security Info=True;User ID={username};Password={password}"
    providerName="System.Data.SqlClient"/>
</connectionStrings>
Adam Miklosi
  • 764
  • 5
  • 18
  • 28
Péter Mag
  • 41
  • 1
  • 4
  • related: https://stackoverflow.com/questions/1838619/relocating-app-config-file-to-a-custom-path – mcalex Mar 21 '19 at 08:44
  • Question: have you tried using `file="path\to\file"` instead of `configSource="\path\to\file"` in `App.config`? – s.m. Mar 21 '19 at 08:44
  • I've tried with this, but it throws an "Unrecognized attribute 'file' " error – Péter Mag Mar 21 '19 at 08:50

1 Answers1

2
  1. don't use file attribute, use configSource - because file works only for appSettings. That too, for appSettings: ConfigSource is complete replacement (no merging)
  2. configSource must refer to a file in the same directory or in a subdirectory as the configuration file.

ASP.NET web.config: configSource vs. file attributes

Zsmaster
  • 1,549
  • 4
  • 19
  • 28