Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include the App.config info in a reference executable in .NET?

I have one executable project, let's say A, which is launching another executable project Bin the run. In order to have a B.exe in A's current working folder, I add B as A's reference so that after the compilation a B.exe will be copied into A's folder. However, I noticed the configuration that I make for B is not copied or generated in A's folder (there is no B.exe.config file in A's folder, only B.exe), and hence the stuff such as tracing for B is not configured correctly.

I can of course copy the B.exe.config manually to A's folder, but I bet there is some automatic way to do that. Could anybody help me?

like image 453
tete Avatar asked Apr 04 '12 14:04

tete


2 Answers

You can use post-build events, or...

In Project A, add a link reference to B.exe.config. You do this by adding an existing item to the project. But, before pressing the Add button on the file dialog, press the down arrow on the right of the Add button and select "Add as Link." Then, set the file to copy to the output directory. In your project file, it will look something like this:

From ProjectA.csproj:

<None Include="..\ProjectB\bin\Debug\B.exe.config">
  <Link>B.exe.config</Link>
  <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>

If you don't mind manually editing your project file, the included file can depend on the build configuration. The following works for builds (though VS2013 will not open the file when you double-click on the icon in the project tree.)

<None Include="..\ProjectB\bin\$(Configuration)\B.exe.config">
  <Link>B.exe.config</Link>
  <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
like image 120
Wallace Kelly Avatar answered Sep 29 '22 01:09

Wallace Kelly


A much simpler solution would be to simply include the following in the csproj file. This includes the matching file extensions for the referenced files.

  <PropertyGroup>
    <AllowedReferenceRelatedFileExtensions>
      .pdb;
      .xml;
      .exe.config;
      .dll.config
    </AllowedReferenceRelatedFileExtensions>
  </PropertyGroup>
like image 28
Telavian Avatar answered Sep 29 '22 02:09

Telavian