Link to home
Start Free TrialLog in
Avatar of John Bolter
John Bolter

asked on

Help with app.config in C# program

Hi, I am trying to merge common content external content into a single app.config. I cannot get it to work. Here is what I do

1. Start/Visual Studio 2013/etc and select new Console Application project
2. My source code looks like shown below and is saved at c:\John\ConsoleApplication1 and directories underneath that.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

Open in new window


My project app.config file looks like this

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <linkedConfiguration href="file://C:\John\ConsoleApplication1\sharedConfig1.config"/>
  </assemblyBinding>

  <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
   </startup>
</configuration>

Open in new window


3. I create a sharedConfig1.config file as referenced above and place it in c:\john\consoleapplication1. Its contents look like this

<test1>
   <test2>
    <add key="test3" value="test4" />
   </test2>
  </test1>

Open in new window


4. I compile and build the application. When I open up C:\John\ConsoleApplication1\ConsoleApplication1\bin\Debug\ConsoleApplication1.exe.config is contains this
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <linkedConfiguration href="file://C:\John\ConsoleApplication1\test.config"/>
  </assemblyBinding>

  <startup> 
        <supportedRuntime version="v4.0" sk[url="http://msdn.microsoft.com/en-us/library/ms404300(v=vs.110).aspx"]http://msdn.microsoft.com/en-us/library/ms404300(v=vs.110).aspx[/url]u=".NETFramework,Version=v4.5" />
   </startup>
</configuration>

Open in new window



My question is why? Why doesn't it include the information in C:\John\ConsoleApplication1\sharedConfig1.config

According to http://msdn.microsoft.com/en-us/library/ms404300(v=vs.110).aspx I have done everything correctly. According to the MSDN link, it says "All linked configuration files are merged to form one file, similar to the behavior of the #include directive in C/C++". This is exactly what I want I just cannot get it to work.

All help or even ideas really welcome.

Thank you

John
Avatar of Mike Eghtebas
Mike Eghtebas
Flag of United States of America image

In Object Browser, select to see what is "Copy to output directory" property when App.config is selected. The options are:

- never copy
- copy if newer
- always copy

If you select one of the last two, App.config should be copied to your Debug folder. Visual Studio never changes the original files (as far as I know). It changes them in compile time if directed to do so.

Mike
Avatar of John Bolter
John Bolter

ASKER

HI, thanks, but that doesn't merge the XML config files which is what I want.
Basically I want the two XML config files to be merged like #include like is says in the MSDN documentation.
Does this not work
It does that on run-time I believe  and puts in Debug folder. After you run, check your Debug folder to see if the combined App.config is there.
Hi eghtebas, it can't do it at runtime because all the source code config files for merging aren't deployed. I have tested it too, and unless my code is bugged, the configuration files defined in the shared file aren't there.

Also according to this MSDN link http://msdn.microsoft.com/en-us/library/ms404300(v=vs.110).aspx it says All linked configuration files are merged to form one file, similar to the behavior of the #include directive in C/C++.

I just don't think it works. I have googled a lot and the only hits I get are other people not getting this to work either :-( Some people talk of something named fuslogvw and fusion only recognising this linkedConfiguration syntax, but that isn't written on the Microsoft website.
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Saige, thank you again, this single answer has made my subscription to Experts Exchange worth the money.

From the documentation on MSDN, it is misleading. Although I think I now understand the purpose of <linkedConfiguration> from your feedback, I can also see to it isn't what I am looking.

Thank you!
Thank you!